Http://www.cnblogs.com/pengmengnan/p/6717766.html
One: Factory mode
The factory pattern is primarily to provide an over-interface for creating objects so that the specific
The process shielding is isolated to achieve the purpose of increasing flexibility.
The factory model can be divided into three categories:
(1) Simple Factory mode (easy Factory)
(2) Factory mode (Factory method)
(3) Abstract Factory mode (factory)
(a): Simple Factory mode
Also known as the Static factory method model for the production of any product in the unified registration structure, by
Build a factory (a function or a class method) to create a new object.
Pattern Composition Structure:
Abstract Product: He generally regards a particular product as inheriting the parent class or the implemented interface. In the Java generation
The code is implemented by an interface or an abstract class.
Product: Factory The object created by the class is an instance of this role. In Java there is a
A specific class to implement.
Factory class: This is the core of this model, contains certain business logic and judgment logic. In
In Java it is often implemented by a specific class.
Example code:
Abstract Products:
public interface car{
void run ();
}
Specific Products:
public class Audi implements Car {
public void Run () {
System.out.println ("Audi car");
}
}
public class Benchi implements car{
public void Run () {
System.out.println ("Mercedes-Benz");
}
}
Factory class:
public class simplefactory{
Public Car Creatercar (String type) {
if ("Audi". Equals (type)) {
return new Audi ();
}else if ("Mercedes". Equals (type)) {
return new Benchi ();
}else {
return null;
}
}
}
Customer Class (test Class):
public class Customer {
public static void Main (String [] args) {
Simplefactory factory = new Simplefactory ();
Car car1 = factory. Creatercar ("Audi");
Ca r car2 = factory. Creatercar ("Mercedes-Benz");
}
}
This is a simple factory model, but the factory part is not ideal, as each additional new
The creation of business logic in the factory, which is clearly against the
. It is conceivable that the factory class is passive for the addition of new products.
So the factory method pattern appeared, and the factory class was defined as an interface, and each new model
class, it is possible to increase the type of plant that corresponds to the
Instead of having to modify the source code.
(ii): Factory method mode
The factory method model is the further abstraction and generalization of the simple factory model, the plant method model
In a single factory class determines which product is instantiated, this decision is given to
The subclass of the abstract factory to do.
Pattern Composition Structure:
Abstract Product: He generally depends on the specific product integration of the parent class or implemented interface. In Java
There are interfaces or abstract classes to implement.
Specific product: The object created by the factory class is an instance of this role. In Java, there are
A concrete class implementation.
Abstract Factory: This is the core of the factory method pattern, which is independent of the application. is specific
The interface that the factory role implements or the parent class that must inherit. In Java by interface or abstract
class to implement.
Specific factory: It contains code that is relevant to the specific business logic. There are application calls
To create the corresponding object for the specific product.
Example code:
Abstract Products:
public interface car{
void run ();
}
Specific Products:
public class Audi implements Car {
public void Run () {
System.out.println ("Audi car");
}
}
public class Benchi implements car{
public void Run () {
System.out.println ("Mercedes-Benz");
}
}
Abstract Factory:
public interface carfactory{
Car Createcar ();
}
Specific factory:
public class Auifactory implements carfactory{
Publuic Car Createcar () {
return new Audi ();
}
}
public class Benchifactory implements carfactory{
Publuic Car Createcar () {
return new Benchi ();
}
}
Customer class:
public class Customer {
public static void Main (String [] args) {
Car car1 = new auifactory. Createcar ();
Car car2 = new. Benchifactory. Createcar ();
}
}
Simple Factory mode vs Factory method mode
(1) Structural complexity: Simple Factory mode is simpler
(2) Code complexity: less Simple Factory mode
(3) Difficulty in management: The core of the factory approach pattern is an abstract factory class, not a simple
Factory model, put the core on a class to expand better, easy to manage
We recommend using the factory method model according to the design concept, but in fact, we generally use the simple
Single-Factory mode.
(iii) abstract Factory mode:
Abstract Factory mode provides an interface that creates a series of related or dependent objects, without
You need to specify their specific class. He was targeting a hierarchy of multiple products.
Reflection mechanism
According to the factory pattern above, according to the requirements to write different object instantiation
method, so spring refers to the idea of reflection:
According to the class name can find its corresponding class and instantiate the object, return an object of the
Class.
Example code:  
Factory class:
public static Object Getbean (String N) {
if (n!=null&&! "". Equals (n)) {
try {
return
Class.forName (n). newinstance ();//Reflection by class name
Object
} catch
(instantiationexception e) {
E.printstacktrace ();
} catch
(illegalaccessexception e) {
E.printstacktrace ();
} catch
(ClassNotFoundException e) {
E.printstacktrace ();
}
}
return null;
}
Customer class:
Idcard u = (idcard) Factory.getbean
("Com.jk.pojo.IdCard");
Because it returns an object, it's going to go strong.
Two: Single case mode
One: a hungry man mode
Characteristics:
Initialize the singleton when the class is loaded, and sometimes not call getinstance
method, invoking another static method, also causes the class to load statically, but at this point it does not
To instantiate Singleton, resulting in a waste of resources.
This approach avoids multi-threaded synchronization problems based on the classloder mechanism, but
Instance is instantiated when the class is loaded, although there are many reasons for class loading,
Most of the getinstance methods are called in Singleton mode, but they are not
Other ways (or other static methods) cause the class to load, the initial
Instance obviously did not achieve the effect of lazy loading.
code example:
1. public class Singleton {
2. private static Singleton instance = new
Singleton ();
3. Private Singleton () {}//privatization with reference to structure
4. public static Singleton getinstance () {
5. return instance;
6.}
7.}
II: A hungry man model variants
The surface looks very different, in fact, the third way is similar, are in the class initial
Instantiate instance. Just put the instantiated object in a static block of code
Example code:
1. public class Singleton {
2. Private Singleto+n instance = null;
3. Static {
4. Instance = new Singleton ();
5.}
6. Private Singleton () {}
7. public static Singleton getinstance () {
8. Return this.instance;
9.}
10.}
Three: Lazy mode (thread-safe)
(1)
Features: Lazy mode without ysnchronized, is thread unsafe, for example
A thread came to judge instance to be null, to instantiate Singleton, in real
Before the example, this is another thread to judge the instance, and the discovery is also null
To instantiate again, so strictly speaking no synchronized lazy mode,
is not a singleton mode. Synchronized is to let the client of the visit automatically queue so that
So that the front has been judged not to be empty, but the subsequent access to a single row
Team visits, so inefficient
Example code:
1. public class Singleton {
2. private static Singleton instance;
3. Private Singleton () {}
4. public static synchronized Singleton
GetInstance () {
5. if (instance = = null) {
6. Instance = new Singleton ();
7.}
8. return instance;
9.}
10.}
(2) Double check Lock (emphasis)
The lazy mode with double-check lock can not only make the thread safe without affecting the efficiency, because
For the first time the first object is queued, the following access is directly
An If intercept, so there's no need to go in line to access
Sample code
public class Singleton {
Private Singleton () {};//privatization constructor
Private static volatile Singleton instance = null;
Volatile instability
public static Singleton getinstance () {//
Synchronized Sync Lock
if (instance==null) {//double-decision Lock
Synchronized (Singleton.class) {
if (instance ==null) {
Instance = new Singleton ();
}
}
}
return instance;
}
}
Four: Lazy type (thread unsafe)
This kind of writing lazy loading is obvious, but the fatal is in multithreading not normal
Job. A thread, for example, determines that instance is null and instantiates
Singleton, before instantiation, this is another thread to judge this
instance, the discovery is also null so again to instantiate once, so strictly speaking not
Plus synchronized lazy mode, not a singleton mode.
1. public class Singleton {
2. private static Singleton instance;
3. Private Singleton () {}
4.
5. public static Singleton getinstance () {
6. if (instance = = null) {
7. Instance = new Singleton ();
8.}
9. return instance;
10.}
11.}
V: (Static inner Class)
1. public class Singleton {
2. private static class Singletonholder {
3. Private static final Singleton INSTANCE = new
Singleton ();
4.}
5. Private Singleton () {}
6. public static final Singleton getinstance () {
7. return singletonholder.instance;
8.}
9.}
This approach also leverages the Classloder mechanism to ensure initialization
Instance has only one thread, which differs from the third and fourth ways (very
Subtle differences): The third and fourth way is as long as the Singleton class is loaded
, then the instance will be instantiated (without the lazy loading effect),
The Singleton class is loaded, and the instance is not necessarily initialized.
Because the Singletonholder class is not actively used, only the display is displayed by invoking the
GetInstance method, the Load Singletonholder class is displayed so that the actual
Instantiate instance. Imagine if the instantiation of instance is very resource-intensive, I
Want him to delay loading, on the other hand, I don't want to be in the Singleton class when loading
Instantiation, because I cannot ensure that the Singleton class may be active in other places
Used to be loaded, it is obviously inappropriate to instantiate instance at this time.
At this point, this approach is reasonable compared to the third and fourth methods.
VI: Enumeration
1. Public enum Singleton {
2. INSTANCE;
3. public void Whatevermethod () {
4.}
5.}
This approach is advocated by effective Java author Josh Bloch, which not only
Avoids multi-threaded synchronization problems, but also prevents deserialization of new objects from being recreated
, is a very strong barrier ah, but, personally, because of 1.5 to join the enum
characteristics, written in this way is not so strange, in practical work, I also seldom
I saw someone write this.
Design patterns used in spring