First, the factory model
The key to Factory mode is to separate the creation and use of objects.
(Easy to modify, if one day you create the object changed the way, then only need to modify the factory)
Cases
For example, I want to build a BMW and let it run, we used to write a BMW class and then directly in this class to instantiate an object of his, after the end of the next to write a BMW car running method, right?
But how does the factory model do this? Take a look at the following steps:
1. Write a car's interface and define a car's go () method inside it:
Public interface Car {
public void Go ();
}
2. Write a BMW class, let him inherit the total interface of the car above, and implement the method inside:
public class BMW implements car{
public void Go () {
}
}
3. What's missing? Oh you have the car interface and implementation class of course there must be a car factory, right? So we also define to it a car factory total interface as well as a BMW car factory that inherits and implements it:
Public interface Carfactory {
Public Car Build (String name);
}
public class Bmwfactory implements carfactory{
private static Bmwfactory ins;
public static Bmwfactory Getins () {
if (null==ins) {
Ins=new bmwfactory ();
}
return ins;
}
Bmwfactory () {}
Public Car Build (String name) {
if (Name.equals ("BMW")) {
return new BMW ();
}
return null;
}
}
4. Some people will be a little grumble, I do not want to always run the factory, so much trouble! Right? Well, then we'll write him a car dealer:
public class Agentfactor {
private static Agentfactor ins;
public static Agentfactor Getins () {
if (ins==null) {
Ins=new Agentfactor ();
}
return ins;
}
Make a brand-name car
Public carfactory Getcarfactor (String Name) {
if (Name.equals ("BMW")) {
return new Bmwfactory ();
}
return null;
}
}
5. OK, the car has, the car factory has, even the agents have to you, then let us write a main function to let our BMW car run up!
public class Cartest {
public void driver () {
Carfactory ic=agentfactor.getins (). Getcarfactor ("BMW");
Car Mini=ic.build ("mini");
Mini.go ();
}
}
Two single-case patterns
The singleton pattern is divided into three kinds: lazy type single case, a hungry man type single case, registration type single case three kinds.
The singleton mode has the following characteristics:
1, the Singleton class can have only one instance.
2, the Singleton class must create its own unique instance.
3, the Singleton class must provide this instance to all other objects.
< Span style= "margin:0px; padding:0px; Color:black; line-height:1.8; Font-family: the song body; >
One, lazy type single case
Lazy-Type singleton class. Instantiate yourself public class Singleton { //private default constructor private Singleton () {} ///Note that there is no final at the first call private static Singleton single=null; Static Factory method Public static Singleton getinstance () { if (single = null) {Single = new Singleton (); } return single; }}
Singleton by restricting the construction method to private to prevent the class from being instantiated externally, the unique instance of Singleton can only be accessed through the getinstance () method within the same virtual machine scope.
(In fact, the Java reflection mechanism is the ability to instantiate a class constructed as private, which basically invalidates all Java Singleton implementations.) This problem is not to be discussed here, but deceiving that the reflection mechanism does not exist. )
However, the above-mentioned lazy singleton implementation does not take into account the thread safety problem, it is thread insecure, the concurrency environment is likely to have multiple singleton instances, the following a hungry man-type singleton is thread-safe.
Two, a hungry man type single case
A hungry man type Singleton class. At class initialization, the public class Singleton1 { //private default constructor child private Singleton1 () has been instantiated by itself. Private static final Singleton1 single = new Singleton1 (); Static Factory method Public static Singleton1 getinstance () { return single; }
a hungry man in the creation of a class at the same time has created a static object for the system to use, no longer change, so it is thread-safe.
III. Registration of single cases
Similar to the method in spring, the class name is registered, the next time from the inside to get directly. public class Singleton3 {private static map<string,singleton3> Map = new hashmap<string,singleton3> (); static{Singleton3 single = new Singleton3 (); Map.put (Single.getclass (). GetName (), single); }//protected by default constructor protected Singleton3 () {}//static Factory method, return the unique instance of this class public static Singleton3 getinstance (String name) { if (name = = null) {name = Singleton3.class.getName (); System.out.println ("name = = NULL" + "--->name=" +name); } if (Map.get (name) = = null) {try {map.put (name, (Singleton3) class.forname (name). newins Tance ()); } catch (Instantiationexception e) {e.printstacktrace (); } catch (Illegalaccessexception e) {e.printstacktrace (); } catch (ClassNotFoundException e) {e.printstacktrace (); }} return Map.get (name); }//A schematic commercial method public StriNg about () {return "Hello, I am Regsingleton."; } public static void Main (string[] args) {Singleton3 single3 = singleton3.getinstance (null); System.out.println (Single3.about ()); }}
A registered Singleton actually maintains an instance of a set of singleton classes that are stored in a map (the register), returned directly from the map for an instance that has already been registered, and then registered and returned, if not registered.
A hungry man and lazy-type difference
These two look very similar at first glance, in fact there is a difference, the main two points
1. Thread Safety:
A hungry man-type is thread-safe, can be used directly for multithreading without problems, lazy-style is not, it is thread insecure, if used for multithreading may be instantiated multiple times, lost the role of Singleton.
If you want to use the lazy type for multi-threading, there are two ways to ensure security, one is to synchronize the GetInstance method, and the other is to use the Singleton method before and after the double lock.
2. Resource loading:
A hungry man a static object is instantiated at the same time as the class is created, regardless of whether the singleton will be used later, it takes up a certain amount of memory, and the corresponding speed will be faster at the time of the call.
And the lazy type as the name implies, will delay the loading, in the first use of the singleton when the object will be instantiated, the first time off to initialize, if you want to do more work, performance will be some delay, after the same as the A hungry man-style.
Java Common Design Patterns