Design Pattern-02-factory method pattern analysis this article mainly refer to from "Java and pattern" and "Zen of Design Pattern" I. Definition
1. Postpone the actual creation to the subclass of the factory interface.
Ii. Advantages and disadvantages of a simple factory
1. insufficient support for the open/closed principle: the client supports the open/closed principle, but the factory itself does not support the open/closed principle. If a new product comes in, you need to modify the factory class accordingly.
Iii. Introduction of the factory method model
1. The factory method mode is further abstracted and improved by the simple factory mode. The factory mode overcomes the problem of insufficient support for the simple factory ratio principle. 2. The core factory class is no longer responsible for the creation of all products. The specific creation work is handed over to the subclass, and the core class becomes the abstract factory role. 3. New products can be introduced without modifying the specific factory role. 4. Usually there is a product role first and then a factory role. That is to say, a factory level structure with the same structure is usually designed according to the product level structure.
V. UML diagram
1. Class Diagram
2. Time Sequence Diagram
Vi. Roles
1. Abstract Factory 2. Specific factory 3. Abstract product 4. Specific product
VII. Code Implementation 1. abstract factory class
package com.jue.dp;public interface IFactory {IProduct newInstance();}
2. Specific factory type
package com.jue.dp;public class ConcreteFactoryA implements IFactory {@Overridepublic IProduct newInstance() {return new ConcreteProductA();}}
package com.jue.dp;public class ConcreteFactoryB implements IFactory {@Overridepublic IProduct newInstance() {return new ConcreteProductB();}}
3. Abstract Product
package com.jue.dp;public interface IProduct {void display();}
4. Specific product categories
package com.jue.dp;public class ConcreteProductA implements IProduct {@Overridepublic void display() {System.out.println("ConcreteProductA->display");}}
package com.jue.dp;public class ConcreteProductB implements IProduct {@Overridepublic void display() {System.out.println("ConcreteProductB->display");}}
5. Test class
package com.jue.dp;public class MainClass {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubIFactory fa = new ConcreteFactoryA();IProduct pa = fa.newInstance();pa.display();}}
8. Advantages
1. In line with the open and closed principles, if the system needs to add a new product, you need to add a product class to the system and the corresponding factory. Excellent scalability. 2. blocked the creation of product classes.
9. Disadvantages
1. Too many products may cause class expansion.
10. Use Cases
1. Do not care about the object creation process, directly care about the created object, and consume the object. 2. Use the factory method mode when using a flexible and scalable framework.
11. Application in Java
1. Collection and iterator Interfaces
2. url and urlconnection applications
The sequence diagram is as follows:
12. Expansion of the factory method model
1. degradation to a simple factory model: a module requires a factory class as much as possible, and there is no need to produce it. You can simply use static methods. 2. cache. After an object is used up, it does not need to be released immediately. The factory class maintains its State so that it can be used again.