1. The singleton model is divided into a hungry man mode and lazy mode;
A Hungry man type:
public class singleton{
private static Singleton Singleton = new Singleton ();
Private Singleton () {}
public static Singleton getinstance () {
return singletion;
}
}
Lazy Type:
public class singleton{
private static Singleton Singleton = null;
public static synchronized synchronized getinstance () {
if (singleton==null) {
Singleton = new Singleton ();
}
return singleton;
}
}
A hungry man is thread-safe, creating a static object at the same time as the class is created for use by the system, and not changing the lazy type. If you create an instance object without adding synchronized, the access to the object is not thread-safe. It is recommended to use the first type.
2. Factory model is divided into simple Factory mode and factory method mode
(1) Simple Factory mode is a specific class to create instances of other classes, the parent class is the same, the parent class is specific.
(2) The factory method pattern is that there is an abstract parent class that defines the public interface, and the subclass is responsible for generating the concrete object, which is intended to defer the instantiation of the class to the child class.
(3) Abstract Factory mode provides an interface to create a series of related or interdependent objects without specifying their specific classes. It is designed for hierarchical structures with multiple products. And the factory method model is for a product grade
Structure.
Singleton mode and Factory mode