The source of this article see: Https://github.com/get-set/get-designpatterns/tree/master/factory-method
The factory method pattern, like the simple Factory mode, is also the creation of the class pattern, also called the virtual Constructor mode or the Polymorphic factory (polymorphic Factory) mode. The intent is to define a factory interface that creates product objects, deferring the actual creation to subclasses.
In the previous article, the Simple factory model did not complete the "open and close principle". In retrospect, the "open" is open to expansion, this is true, one of the original purpose of the simple factory model is to facilitate the increase of "product type", "closed" is the modification of the closure, which is not done, when the need to add or change "product type", the factory class must be modified, because the factory class "know" How to create an object of all product types.
Then the factory method model is to fully meet the "open and close principle", that is, on the basis of a simple factory model, when adding product types, do not need to change the existing code. Continue with the example of the previous article:
Example
is still a drawing software that can draw rectangles, triangles, and circles, each of which is managed by a class:
* rectangle
* circle
* triangle
Each class has its own draw () method, common implementation shape interface.
Shape.java
public interface Shape {void Draw ();}
Rectangle.java
public class Rectangle implements Shape {@Override public void Draw () {System.out.println ("draw a Rectangle.") ; }}
Triangle.java
public class Triangle implements Shape {@Override public void Draw () {System.out.println ("draw a Triangle."); }}
Circle.java
public class Circle implements Shape {@Override public void Draw () {System.out.println ("Draw a circle."); }}
None of the above classes have changed, the factory class is changed, and the factory class is also based on the interface design, which is responsible for the creation of the corresponding objects by different specific factory classes:
Shapefactory.java
Public interface Shapefactory {Shape getshape ();}
Rectanglefactory.java
public class Rectanglefactory implements Shapefactory {public Shape getshape () {return new Rectangle (); }}
Circlefactory.java
public class Circlefactory implements shapefactory{public Shape Getshape () {return new Circle (); }}
Triangle.Factory.java
public class Circlefactory implements shapefactory{public Shape Getshape () {return new Circle (); }}
Then, when a shape is needed, it can be created by the corresponding specific factory class:
Client.java
public class Client {public static void main (string[] args) {Shapefactory factory = new Circlefactory (); Shape C = Factory.getshape (); C.draw (); }}
Let's take a look at the class diagram:
650) this.width=650; "src=" https://leanote.com/api/file/getImage?fileId=5a09a297ab6441546d002770 "title=" "style=" Border:none;margin-top:15px;margin-bottom:15px;height:auto; "/>
Compare the following with the Simple Factory mode:
650) this.width=650; "src=" https://leanote.com/api/file/getImage?fileId=5a09a297ab6441546d002772 "title=" "style=" Border:none;margin-top:15px;margin-bottom:15px;height:auto; "/>
The difference is that a specific factory class in the simple Factory mode translates into ShapeFactory three specific factory classes based on the interface.
The benefits are also obvious: when adding a new shape type, you do not need to make any changes to the existing code, adding a corresponding implementation ShapeFactory of the specific factory class. It can be seen that the factory method model is able to fully achieve the "open and close principle".
This example is still inappropriate because the pattern design is more complex than the original implementation in any case. So repeat the note again, the above example is to illustrate the factory method pattern, but not a reasonable application. complex objects are suitable for use in Factory mode, and simple objects, especially those created with new, do not need to use Factory mode. If you use Factory mode, you need to introduce a factory class that increases the complexity of the system.
Applications in Java
Take a look at the reasonable scenario, the practical example in Java (from Java and schema):
* 1. Applications in Java Aggregation *
Java aggregation is a well-designed data structure implementation, the main Java aggregation is implemented from the java.util.Collection interface, the interface's parent interface Iterable stipulates that all Java aggregation must provide a iterator() method to return a Iterator type of object:
Java.lang.Iterable.java
Public interface Iterable<e> {... Iterator<e> Iterator (); ... ...}
Arraylis is one of the collection implementation classes we use, and its iterator () method is implemented as follows:
Java.util.ArrayList.java
public class arraylist<e> extends abstractlist<e> implements List<E>, RandomAccess, Cloneable, java.io.serializable { ... ... // implements the Iterato interface, Returns a Iterator object public iterator<e> iterator () { return new itr (); } PRIVATE&NBSP;CLASS&NBSP;ITR&NBSP;IMPLEMENTS&NBSP;ITERATOR<E>&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;...&NBSP, ..... &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;...&NBSP, ...}
It can be seen that the ArrayList iterator() method of a class is a factory method of a specific factory class, which Collection is an abstract factory. In addition to ArrayList having to LinkedList wait for a specific implementation class.
Summarize
-
has achieved a complete" open and close principle "because of the addition of new" Products "and corresponding" factories " There is no need to modify existing code; the
-
Factory design pattern is typically applied in complex object creation scenarios, and therefore faces multiple layers of inheritance, such as arraylist implements the list interface, while the latter inherits from collect interface, collect also inherits from iterator interface. Sometimes specific factory methods and specific products are hierarchical correspondence, such as:
650) this.width=650; "src=" https://leanote.com/api/file/getImage?fileId=5a09a297ab6441546d002771 "title=" "style=" Border:none;margin-top:15px;margin-bottom:15px;height:auto; "/>
This is also the case that only a simple factory model of a factory class can be satisfied.
Java Design Pattern Hundred cases-factory method mode