Design Model (Creation)
Directory
- Integration of creative design patterns (builder mode + factory mode/static factory + Abstract Factory mode) template creation implementation
- Simple Factory
- Factory Model
- Abstract Factory Model
- Builder Mode
- Prototype
- Singleton mode (next article)
Tucao: weekend + Mid-Autumn Festival + birthday, to write code at home tomorrow, we have wood ................................. ..........
No eclipse is installed on this computer, it can only be handwritten ..................................... .............
1. Creation-type design mode integration (builder mode + factory mode/static factory + Abstract Factory mode) template creation implementation
Personal Summary: The creative design pattern is generally used to provide the business layer or logic layerBottom LayerImplementationClass initializationPolicy design ideas.
To put it simply: The mode used to create objects.
My strategy for this integration:
Abstract Factory mode: used to create objects
Builder mode: used to initialize objects
Factory mode/static Factory: Used to abstract the builder module again (the code here will be described in a simple factory)
Procedure:
Scenario preparation: a bunch of messy implementation classes (A1, A2, A3) (B1, B2, B3) (C1, C2, C3)
Overall Design Steps->1. Abstract implementation classes and abstract general features (this is the core of the Creative Design Model)
(A1, A2, A3) abstract interface A, (B1, B2, B3) abstract interface B .......
2. Define one or more interfaces. There is a method used to obtain the subclass of the, B, and C interfaces abstracted in the first step ....
Interface managerservice {public a getainstance ();
Public B getbinstance ();
Public C getcinstance ();
....
}
3. Compile the implementation classes factory1, factory2, and factory3 of the managerservice interface;
4. Each factory can freely combine different implementation classes when implementing the getinstance method, such
@ Override
Public A getainstance (){
Return new A1 (); // return New A2 ();
} ------ This is the framework of the factory model.
/** Extension code -- used to initialize the attributes of the new output object
Through the encapsulation of this layer, the most important thing is not only to create a new implementation Class A1, B2, C1...
But for more extensions: the extension of the attributes of the initialization object.
1. Define an interface user build object attribute
Interface builder {
Public A bulid ();
}
2. buildersamplea and buildsampleb
@ Overrider
Buildersamplea {
A;
Public buildersamplea (A) {This A = ;}
Public A bulid (){
A. setxxx
A. setxxx
....
}
}
3. Define a control class. Create is used to control the method to be initialized. It receives the implementation subclass of build.
Public class create {
Public object execute (builder B ){
Return B. Build ();
}
}
4. Each factory implements the getinstance method, which can be compared with the implementation before the above Code is extended.
@ Override
Public A getainstance (){
// Return New A1 ();
Create c = new create (NewBuildersamplea(New A1 (); // This factory initializes A1, so only the sub-classes of builder can be changed here, and the builder of samplera can continue to abstract and factory using the above method.
Return c.exe cute () // But I personally suggest that you consider using a static factory method at this level. After all, there are not many policies for initializing parameters, so we will not extend them here.
}
**/
5. Client call process-Minimum awareness principle: expose the managerservice interface and the specific factory that completes the call.
This is an example of creating an A1 implementation class. For example, factory1 and 2,3 correspond to A1, 2, 3, and 3 respectively.
Managerservice manager =New factory1 (); // no matter which type of a implementation class is created, it must be modified only when it is marked red.
A A = manager. getainstance ();
/** The extension is the same for the client. Maybe the input parameters of the getainstance (string type) method are different. Which sampler type is used to select?
Managerservice manager = new factory1 ();
A A = manager. getainstance ("samplera ");
**/
After reading the UML diagram, let's split the various policies of the entire creation mode.
Ii. Simple Factory
Picture taken from Baidu encyclopedia
Step 1Abstracts underlying implementation classes into a public interface
Step 2Compile a control layer for creating an instance and create different implementation classes based on different input parameters.
Here I will give a demonstration of the static factory class (hand writing is required, and it is exhausted). For other examples, refer to the above integrated version. (Added the question of selecting buildersamplea, B, and C) public class factory {public static creator (string type) // reflection can be used here to implement {If (XXX. equals (type) return New
Buildersamplea(New A1 (); If (yyy. Equals (type) return New
Buildersampleb(New A1 ();}}
Personal rating
Advantages:The essence of the static factory method is to encapsulate the logic of the client to the server, which is indeed much less for the Code. When the logic is not large and relatively stable, this is a good strategy.
Disadvantages:The disadvantage is also obvious. When the logic is complex, the Code is not highly readable (a lot of IF/else). If you add a service each time, you need to modify the internal class, this seriously violates the open and closed principles.
Solution:Business logic can be decoupled through the factory mode,An if is a factory class, and then abstracts an interface for managing the factory according to the factory class. The client only needs to know the interface and factory class for managing the factory.
Iii. Factory Model
Picture taken from the left Yunlong blog
Step 1Abstract an interface for the underlying implementation class
Step 2Defines a factory management interface for the overall new implementation class
Step 3Implement the factory class corresponding to the number of business scenarios to implement the factory management interface new bottom-layer implementation (equivalent to converting the IF/else in a simple factory into one class to decouple the business logic))
Step 4Because the factory class is responsible for managing the new process and can also be understood as the encapsulation process, we can introduce the -- builder mode here.
Personal rating:Very good sealing layer idea, through the factory sealing layer makes class initialization more convenient.
Disadvantages:I did not think that there may be many factory and product categories, but I think it is necessary.
Iv. Abstract Factory Model
I will not elaborate on the steps here
Equivalent to the factory model,The only difference is that the interface is no longer a single method, so the factory produces a set of components.
Personal ratingIt is equivalent to the factory model.
V. Builder Mode
The builder mode initializes attributes of object classes.
Step 1Define a builder Interface
Step 2The classes that implement the builder interface are actually the initialization parameter policies.
Step 3Create a creator class to manage the sub-classes of the builder in a unified manner,
In fact, here Step 2 can be used. The client is builder B = new buildersamplea (A); B. Build (); This completes the call.
If Step 3 is written, the client is creator c = new creator (New buildersamplea (A a));c.exe cute ();
Do you know why creator is required?
A: Actually, I personally understand it to encapsulate it again. Refer to the example in the static factory method.
Vi. prototype mode
I have seldom used the prototype mode. It generally inherits the cloneable interface and then uses the clone method of the parent class object.
For deep cloning, the objects at the next layer also need to be overwritten.
Therefore, it is not convenient to use this method for complex objects. There may be better policies for the clone method.
I used the JSON Method for cloning, which is quite convenient.
Object --> JSON ---> Object
Main references:
Big talk Design Model
Baidu encyclopedia
Http://www.cnblogs.com/zuoxiaolong/p/pattern26.html
Design Mode (creation type) 1