Design Patterns - Abstract Factory Pattern

Reprinted to http://blog.csdn.net/jason0539/article/details/44976775Example

Background:
With the increasing demands of customers, BMW cars need accessories such as air conditioners and engines with different configurations. So the factory started producing air conditioners and engines, which were used to assemble cars. At this time the factory has two series of products: air conditioners and engines. The BMW 320 series is equipped with A-type air conditioners and A-type engines, and the BMW 230 series is equipped with B-type air conditioners and B-type engines.

Concept:
   The abstract factory pattern is an upgraded version of the factory method pattern, which is used to create a set of related or interdependent objects. For example, the BMW 320 series uses air conditioning model A and engine model A, while the BMW 230 series uses air conditioning model B and engine model B, then using the abstract factory model, when producing related accessories for the 320 series, there is no need to specify the model of the accessories, it will Automatically produce the corresponding accessory model A according to the model.

For the introduction to the abstract factory pattern on Baidu Encyclopedia, the example is as follows:

when each abstract product has more than one concrete subclass (air conditioners have models A and B, and engines also have models A and B) ), how does the factory role know which subclass to instantiate? For example, each abstract product role has two concrete products (the product air conditioner has two concrete products, air conditioner A and air conditioner B). The abstract factory model provides two specific factory roles (BMW 320 series factory and BMW 230 series factory), which correspond to these two specific product roles respectively, and each specific factory role is only responsible for the instantiation of a certain product role. Each concrete factory class is only responsible for creating an instance of a concrete subclass of the abstract product.

Abstract factory pattern code
Product class:
[java] view plain copy
//Engine and model   
public interface Engine {   
 
}   
public class EngineA extends Engine{   
    public EngineA(){   
        System.out.println("制造-->EngineA");   
    }   
}   
public class EngineBextends Engine{   
    public EngineB(){   
        System.out.println("制造-->EngineB");   
    }   
}   
 
//空调以及型号   
public interface Aircondition {   
 
}   
public class AirconditionA extends Aircondition{   
    public AirconditionA(){   
        System.out.println("制造-->AirconditionA");   
    }   
}   
public class AirconditionB extends Aircondition{   
    public AirconditionB(){   
        System.out.println("Manufacturing-->AirconditionB");   
    }   
}  

Create a factory class:
[java] view plain copy
//Create factory interface   
public interface AbstractFactory {   
    //Manufacturing engine 
    public Engine createEngine(); 
    //Manufacturing Air conditioner  
    public Aircondition createAircondition();  
}   
 
 
//Produce accessories for BMW 320 series   
public class FactoryBMW320 implements AbstractFactory{   
       
    @Override   
    public Engine createEngine() {     
        return new EngineA();   
    }   
    @Override   
    public Aircondition createAircondition() {   
        return new AirconditionA() ;   
    }   
}   
//宝马523系列 
public class FactoryBMW523 implements AbstractFactory {   
   
     @Override   
    public Engine createEngine() {     
        return new EngineB();   
    }   
    @Override   
    public Aircondition createAircondition() {   
        return new AirconditionB();   
    }   
 
 
}  

客户:
[java] view plain copy
public class Customer {   
    public static void main(String[] args){   
        //生产宝马320系列配件 
        FactoryBMW320 factoryBMW320 = new FactoryBMW320();   
        factoryBMW320.createEngine(); 
        factoryBMW320.createAircondition(); 
           
        //Production of BMW 523 series parts        FactoryBMW523    factoryBMW523
        = new FactoryBMW523();    factoryBMW320.createEngine 
        (); 
        factoryBMW320.createAircondition();  It can be understood by repeating the examples, and there are many concepts such as product family and hierarchical structure mentioned, but it is more difficult to understand. The origin of the abstract factory pattern The following is a reference to the origin of the abstract factory pattern: the origin or the earliest application of the abstract factory pattern is used to create windows that belong to different operating systems. For example: Command button (Button) and text box (Text) are both window constructions. In the Windows environment of the UNIX operating system and the Windows environment of the Windows operating system, these two constructions have different local implementations, and their details are different. . In every operating system, there is a family of builds consisting of the Windows builds. Here is the product family composed of Button and Text. And each window component constitutes its own hierarchical structure, an abstract role gives an abstract function description, and a concrete subclass gives the concrete implementation under different operating systems. It can be found that in the product class diagram above, there are two hierarchical structures of products, namely the Button hierarchical structure and the Text hierarchical structure. There are two product families at the same time, namely the UNIX product family and the Windows product family. The UNIX product family consists of UNIX Button and UNIX Text products; the Windows product family consists of Windows Button and Windows Text products.













The system's creation requirements for product objects are met by a project hierarchy, in which there are two specific project roles, namely UnixFactory and WindowsFactory. The UnixFactory object is responsible for creating products in the Unix product family, and the WindowsFactory object is responsible for creating products in the Windows product family. This is the application of the abstract factory pattern. The solution of the abstract factory pattern is as follows:

Obviously, a system can only run in the Windows environment of a certain operating system, but cannot run on different operating systems at the same time. Therefore, the system can actually only consume products belonging to the same product family.
In modern applications, the scope of use of the abstract factory pattern has been greatly expanded, and it is no longer required that the system can only consume a certain product family.


Summary:
Whether it is a simple factory pattern, a factory method pattern, or an abstract factory pattern, they all belong to the factory pattern, and are very similar in form and characteristics, and their ultimate purpose is decoupling. When using it, we don't have to care whether this pattern is the factory method pattern or the abstract factory pattern, because the evolution between them is often unpredictable. Often you will find that the factory method pattern that is clearly used, when new requirements come, slightly modified and added a new method, because the products in the class constitute product families in different hierarchical structures, it becomes the abstract factory pattern. For the abstract factory pattern, when a method is reduced so that the provided product no longer constitutes a product family, it evolves into a factory method pattern.
       Therefore, when using the factory pattern, you only need to care about whether the purpose of reducing coupling has been achieved.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326613811&siteId=291194637