2016-04-24 10:10:34
Create Pattern: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode
Note: The factory model can be divided into three categories: 1) Simple Factory mode (easy Factory) 2) Factory method mode (Factory methods) 3) Abstract Factory mode (abstraction Factory)
These three modes are progressively abstracted from top to bottom and more general. In design mode, GOF divides the factory model into two categories: The Factory method Model (Factory) and the Abstract Factory mode (abstraction Factory). The simple Factory is seen as a special case of the factory method pattern, which is grouped into one category.
Factory method Mode :
(1) Simple Factory mode (also called static Factory method mode)
By specifically defining a class to be responsible for creating instances of other classes, the instances that are created typically have a common parent class.
Roles included in the pattern and their responsibilities:
1. Factory (Creator) role
The core of the simple factory pattern, which is responsible for implementing the internal logic of creating all instances. The factory class can be called directly by the outside world to create the desired product object.
2. Abstract (Product) role
The parent class of all objects created by the simple factory pattern, which is responsible for describing the common interfaces common to all instances.
3. Specific products (concrete product) role
Specific instance objects created by the simple Factory mode
Example: harvesting fruit, such as apples, bananas
Apple and banana are specific product roles, fruit is an abstract role, a public interface between Apple and banana, and fruitfactory is a factory role that is responsible for creating apple and banana instances.
Public Interface Fruit { / * * collect * /public void get ( );}
public class Apple implements fruit{ /* * capture */ public void " Capture Apple "
Public class Implements fruit{ / * * Collect */public void get () { System.out.println ("collect Bananas"); }
Public classFruitfactory {/** Get an example of an Apple class*/ Public StaticFruit getapple () {return NewApple (); } /** Get Banana class instances*/ Public StaticFruit Getbanana () {return NewBanana (); }}
Public class MainClass { publicstaticvoid main (string[] args) { = Fruitfactory.getapple (); = Fruitfactory.getbanana (); Apple.get (); Banana.get ();}
You can also modify the factory method to
Public class fruitfactory { / * * Getfruit method, get all product objects */public Static throws instantiationexception, Illegalaccessexception, classnotfoundexception { = Class.forName (type ); return (Fruit) fruit.newinstance (); }}
This dynamically loads and creates class classes, but does not pay attention to capitalization;
Further modifications to:
Public classFruitfactory {/** Getfruit method to get all product objects*/ Public StaticFruit getfruit (String type)throwsinstantiationexception, Illegalaccessexception, classnotfoundexception {if(Type.equalsignorecase ("Apple")) { returnApple.class. newinstance (); } Else if(Type.equalsignorecase ("Banana")) { returnBanana.class. newinstance (); } Else{System.out.println ("The corresponding instantiation class could not be found"); return NULL; } }}
And then
Public class MainClass { publicstaticvoidthrows instantiationexception, Illegalaccessexception, classnotfoundexception { = fruitfactory.getfruit ("Apple"); = Fruitfactory.getfruit ("Banana"); Apple.get (); Banana.get (); }}
Advantages and disadvantages of the simple factory model:
In this mode, the factory class is the key to the entire pattern. It contains the necessary judgment logic to determine which specific class of objects should be created based on the information given by the outside world. Users can create the required instances directly from the factory class, without having to know how they are created and how they are organized. facilitates the optimization of the entire software architecture.
It is not difficult to find that the shortcomings of the simple factory model are also reflected in its factory class, because the factory class centralizes all instances of the creation logic, so "high cohesion" does not do well. In addition, when the specific product classes in the system are increasing, it may be necessary to require the factory class to make corresponding changes, extensibility is not very good. For example, to add a fruit-pear, you need to continue to write the else if statement in Fruitfactory, not conform to the open closure principle. Consider the following factory method mode.
(2) Factory method mode (also known as polymorphic Factory mode)
The meaning of the factory method pattern is to define a factory interface that creates the product objects, deferring the actual creation to subclasses. The Core factory class is no longer responsible for product creation, so that the core class becomes an abstract factory role and is responsible only for the interfaces that a specific factory subclass must implement, and the benefit of further abstraction is that the factory method pattern allows the system to introduce new products without modifying the specific factory roles.
Roles included in the pattern and their responsibilities:
1. Abstract Factory (Creator) role
At the core of the factory method pattern, any factory class must implement this interface.
2. Specific factory (concrete Creator) role
The specific factory class is an implementation of the abstract factory, which is responsible for instantiating the product object.
3. Abstract (Product) role
The parent class of all objects created by the factory method pattern, which is responsible for describing the common interfaces common to all instances.
4. Specific products (concrete product) role
Specific instance objects created by the factory method pattern
Abstract Factory mode
Single-Case mode
Builder mode
Builder mode, also known as generator mode. The builder mode is one of the object-creating patterns that hides the creation of compound objects, abstracts the creation of compound objects, and dynamically creates objects with composite properties through subclass inheritance and overloading.
Structure of the builder pattern:
Builder Mode Application Scenario:
1. Object creation: Builder mode is a pattern designed for object creation
2. Create a Composite object: the object being created is a composite object with a composite property
3, focus on the creation of the various parts of the object creation process: Different factories (here refers to the Builder builder) have different ways to create product attributes
Prototype mode
Define a person class
Public classPerson {//name PrivateString name; //Age Private intAge ; //Sex PrivateString sex; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString Getsex () {returnsex; } Public voidsetsex (String sex) { This. Sex =sex; }}
Often we use new to create instances
public class MainClass { static void main (string[] args) {person Person1 = new person (); Person1.setname ( "Zhangsan" ); Person1.setage ( 30); Person1.setsex ( "male" ); Person Person2 = new person (); Person2.setname ( "LiSi" ); Person2.setage ( 30); Person2.setsex ( "male" ); }}
The two objects Person1, Person2 except the name, and the others are the same. We don't want to repeat the setup, we want to copy the Person1, we can use the prototype mode.
Prototype Mode application scenario:
1. When creating an object, we don't just want the object being created to inherit the basic structure of its base class, but also to inherit the data from the prototype object.
2, I hope that the modification of the target object does not affect the existing prototype objects (deep cloning can be completely unrelated to each other).
3, hide the details of the cloning operation. Many times, the cloning of the object itself needs to involve the data details of the class itself.
Features of the prototype model:
1. The target object is created by the prototype object itself. That is, object creation is an action that originates from the prototype object itself.
2. The target object is a clone of the prototype object. That is, objects created through the prototype pattern have the same structure as the prototype object and have the same values as the prototype object.
3. Based on the different depth levels of the object cloning, there are shallow clones and deep clones.
Superficial cloning:
Deep cloning:
Java design pattern--Create pattern