Three minutes to learn how to use the singleton mode, three minutes to learn

Source: Internet
Author: User

Three minutes to learn how to use the singleton mode, three minutes to learn

The Singleton mode is one of the simplest development modes and one of the most commonly used development modes.

 

Use Cases include database connection, file operations, or sharing.

 

 

The following is the code details, which can be understood after three minutes.

 

1 package designMode_java.singleton; 2 3/** 4 * singleton 5 * @ author anhelida 6*7 */8 public class SingletonBean {9 10 // instantiate object 11 private static final SingletonBean SINGLETON_BEAN = new SingletonBean (); 12 13 // The instance object 14 private SingletonBean () {15 16} 17 18 is not allowed in the new way. // The instance 19 public static SingletonBean getSingletonBean () is provided externally () {20 return SINGLETON_BEAN; 21} 22 23 // custom Method 24 public void showInfo () {25 System. out. println ("only one Singleton class, only one instance allowed"); 26} 27 28}

 

If you use new to create an instance when using this class, an error is reported,

 

 

The correct use of So is:

1 package designMode_java.singleton; 2 3 public class Test {4 public static void main (String [] args) {5 // get object instance 6 SingletonBean singletonBean = SingletonBean. getSingletonBean (); 7 // execution method 8 singletonBean. showInfo (); 9} 10}

 

 

Isn't it easy!

 


Is the factory mode and Singleton mode in java very useful? I don't know much about the template. I just learned it, but I don't know how to do it. What should I do?

Template Method: an abstract class with a final state method (which implements the call sequence of step methods) and n Abstract Step Methods waiting for subclass implementation.
Subclass inherits the abstract class and implements step-by-step methods.
The template method can redefine certain steps in the algorithm without changing the algorithm structure (the final state method in the abstract class) (the step method implemented by the subclass ).

In Class 5 (1), there are 40 people studying paper-cutting. After everyone learns, they will teach others. If they teach the first person, it will take three minutes. How long does it take for the class to learn paper-cutting?

There are 1 in the first three minutes, 2 in six minutes, 2 in nine minutes × 2 = 4, and 8 in twelve minutes. The number of people who will paper-cut in every three minutes doubles. It takes 21 minutes to finish all their studies. It is worth noting that the last three minutes are not one-on-one instructors, but the time will not overlap, so it will take three minutes even if there is only one person left.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.