Design Patterns ----------- Singleton design patterns, Singleton Design Patterns

Source: Internet
Author: User

Design Patterns ----------- Singleton design patterns, Singleton Design Patterns
Hungry Chinese Style: class Single {// do it in advance! Private static final Single s = new Single (); // private constructor cannot use new to create an object! Private Single () {}// public static Single getInstance () {return s ;}
}


Lazy style: As the name implies, lazy guy is lazy. When will it be used to create it!
Class Single1 {private static final Single1 s = null;
Private Single1 (){}

Public static Single1 getInstance () {// when to call and when to assign a value to a lazy if (a = null) // The thread here is not secure! (Not recommended. Try to use the hungry Chinese Style !) S = new Single1 (); return s ;}
}
What design patterns are familiar to you? Write the implementation code of Singleton Mode

There are 23 design modes in total!

Reference the "software secrets-the point of design patterns" book:

According to the purpose, the design patterns can be divided into creation patterns, structural patterns, and behavior patterns.
The creation mode is used to process the object creation process, the structure mode is used to process the combination of classes or objects, and the behavior mode is used to describe how classes or objects interact and how responsibilities are assigned.

The creation mode is used to process the object creation process. It mainly includes the following five design modes:
Factory Method Pattern)
Abstract Factory Pattern)
Builder Pattern)
Prototype Pattern)
Singleton Pattern)

The structural mode is used to process the combination of classes or objects. It mainly includes the following seven design modes:
Adapter Pattern)
Bridge Pattern)
Composite Pattern)
Decorator Pattern)
Facade Pattern)
Flyweight Pattern)
Proxy Pattern)

The behavior pattern is used to describe how classes or objects interact and how responsibilities are assigned. It mainly includes the following 11 design patterns:
Chain of Responsibility Pattern)
Command Pattern)
Interpreter mode (Interpreter Pattern)
Iterator Pattern)
Mediator Pattern)
Memory memorandum mode (Memento Pattern)
Observer Pattern)
State Pattern)
Strategy Pattern)
Template Method Pattern)
Visitor Pattern)

Singleton mode Implementation 1:
Public class Singleton {
// Class shared Instance Object
Private static Singleton singleton = null;
// Private constructor
Private Singleton (){
System. out. println ("-- this is Singleton !!! ");
}
// Obtain the singleton Method
Public synchronized static Singleton getInstance (){
// Determine whether the shared object is null. If it is null, a new object is created.
If (singleton = null ){
Singleton = new Singleton ();
}
Return singleton;
}
}

Singleton mode implementation 2:
Public class Singleton {
// Class shared Instance Object Instantiation
Private s... the remaining full text>

Java Singleton Design Mode

Do you want
Public static void setInstance (Single sigle) // static methods can only call static parameters,
{
S = sigle;
}
This way?
You can add it to the class, but you can see that when you call the setInstance () method externally, You need to input a single instance as a parameter, but this instance is obtained by getInstance, what is the significance of the single Instance generated by the original private static single s = new single?

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.