[Design Mode] factory model (Factory Method)

The purpose mode

A definition of the interface for creating an object, let subclasses decide which class of instances.
Factory method instantiated class model to be delayed subclasses.

Mode structure

component

  • Product (Product)
    - defines the product features of the interface
  • Specific products (ConcreteProduct)
    - specific products to achieve product interface
  • Factory (Creator)
    - defines the functions of the factory interface
  • Specific plant (Concrete)
    - to achieve specific plant factory interface

java achieve

The method of using the plant model, and a Mercedes Benz car factory production, BMW example
automotive products, is defined as the interface ICar
plant automobile plant, defined as the interface ICarFactory
following code UML class diagram:

Use the BMW factory getInstance()method to create a BMW car, using a Mercedes-Benz factory getInstance()method to create a Mercedes.
Related code is as follows:
ICar.java

public interface ICar {
    /**
     * 实现类必须实现run()方法
     **/
    void run();
}

BmwCar.java

public class BmwCar implements ICar{
    @Override
    public void run() {
        System.out.println("This is Bmw running");
    }
}

BenzCar.java

public class BenzCar implements ICar{
    @Override
    public void run() {
        System.out.println("This is Benz running");
    }
}

ICarFactory.java

public interface ICarFactory {
    /**
     * 实现类必须实现getCar()方法
     **/
    ICar getCar();
}

BmwFactory.java

public class BmwFactory implements ICarFactory {
    @Override
    public ICar getCar() {
        return new BmwCar();
    }
}

BenzFactory.java

public class BenzFactory implements ICarFactory {
    @Override
    public ICar getCar() {
        return new BenzCar();
    }
}

Client test code and output as follows:
Client.java

public class Client {
    public static void main(String[] args) {
        ICarFactory carFactory = new BenzFactory();
        carFactory.getCar().run();

        ICarFactory carFactory2 = new BmwFactory();
        carFactory2.getCar().run();
    }
}

Output:

This is Benz running
This is Bmw running

Mode Advantages

  1. In the factory method pattern, factory method to create products needed by customers, but also to customers which hides the specific product class will be instantiated this detail, users only need to worry about the plant required for the corresponding product without concern created details, or even know the name of the class specific product category.
  2. Polymorphism plant design is based on the role and character of the product is the key factory method pattern. It enables the plant can autonomously determine what product object is created, and how to create the details of the object is completely encapsulated in the concrete factory. The reason why the method of the factory mode is also called polymorphic factory mode, because all of the specific factory classes with the same abstract parent class.
  3. Another advantage of using the factory methods is added to a new product in the system, without modifying the interface abstraction abstract factory and the products, without having to modify client, there is no need to modify the particular plant and other specific products, as long as a particular plant is added and specific products on it. Thus, scalability of the system will become very good, full compliance with "the principle of opening and closing."

Mode shortcomings

  1. When adding a new product, you need to write a new class of specific products, but also to provide the corresponding concrete factory class, the number of pairs of the class system increases, the complexity of the system to some extent, there is more the class needs to be compiled and run, will bring some additional overhead system.
  2. Taking into account the scalability of the system, requires the introduction of abstraction layer, are defined in the abstraction layer using the client code, the system increases the abstraction and the difficulty of understanding, and may need to use the DOM, reflection ART When implemented, increase the difficulty of implementing the system.

to sum up

  1. Factory Method pattern is also known as the factory model, which belongs to the class creates a schema. In the factory method mode, the factory parent class is responsible for defining create products object's public interface, the factory subclass is responsible for generating a particular target products, the aim is to instantiate operation of the product class delay to the plant subclass completed, i.e., by determining whether the factory subclass which a specific product class to instantiate.
  2. Factory Method pattern consists of four roles: abstract product is defined product interface is factory method pattern of objects created super type, that is, the product of a common parent objects class or interface; specific products to achieve the abstract product interfaces, some type of specific products created by specialized plant-specific, often one correspondence between them; the abstract factory declares the factory method, used to return a product, it is the core of the factory method pattern, any factory class to create objects in the schema must implement the interface; concrete factory class is a subclass of abstract factory, factory implements the abstract factory methods defined, can be called by the client and returns an instance of the specific product class.
  3. Factory Method pattern is to further promote the simple and abstract factory pattern. Due to the use of object-oriented polymorphism, a simple holding mode factory method factory pattern advantages and overcome its shortcomings. In the factory method pattern, the core of the factory class is no longer responsible for creating all the products, but will work to create a specific subclass to do it. The core classes are given only responsible for plant-specific interfaces that must be implemented, not responsible for the product class is instantiated such details, which makes factory method pattern can allow the system to introduce new products in the plant without modifying role.
  4. The main advantage of the plant model is a method to modify an existing system without adding new class of products, and packaging products of the details of creating an object, the system has good flexibility and scalability; disadvantage that at the same time add new products need to add new plant, resulting in increasing the number of pairs of system classes, increases the complexity of the system to some extent.
  5. Factory Method pattern applicable, include: a class does not know it needs class objects; a class is specified by a subclass which object is created; to create objects of a particular task entrusted to the client a number of factories in the subclass when used without concern for what may be a sub-class factory to create products sub-category, and then dynamically assigned as needed.

Guess you like

Origin www.cnblogs.com/zhengxl5566/p/12145809.html