Default mode and singleton mode for Java

Source: Internet
Author: User

#模式设计
# #1, default mode (also called default mode)
# # #1.1-There is a monk interface, Monk interface methods have Chizhai, meditation, chanting, martial arts, and there is a LU Zhishen to achieve this interface, but he did not want to implement all the methods in the interface, he only want to use the monk interface in the martial arts method. What about this time?
# # #1.2-Solution: Find an Intermediate class, this class can be an abstract class, or it can be a concrete class, let this class to implement the Monk interface, the middle class for LU Zhishen LU zhishen service, Lu Zhishen need what method, it will implement what method, the other method without tube, and then Lu Zhishen to inherit this intermediate class, After inheritance, only use martial arts this method can be
# # #1.3-Specific implementation:
Lu Zhishen Inheritance —————— > Intermediate class implementation —————— > Monk interface
# #2, Singleton mode
# # #1, lazy-style features:
There can be only one instance (static constructor method private)
Instantiate your own unique instance (self-declared static private)
Promote yourself (externally provide new own public static method)

Private Singleton () {};
private static Singleton Singleton=null;
public static Getsingleton () {
if (singleton==null) {
Singleton=new Singleton ();
}
return singleton;
}
# # #2, a Hungry man single-case mode
Private Singleton () {}
Private static final Singleton single = new Singleton1 ();
Static Factory method
public static Singleton1 getinstance () {
return single;
     }  

Default mode and singleton mode for Java

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.