This is the sixth day of my participation in Gwen Challenge

Abstract factory method pattern

Now that we’ve seen the factory method pattern, what is the abstract factory method pattern? Abstract factories are defined to provide an interface for creating a set of related or interdependent objects without specifying concrete classes. It can be understood that the abstract factory method pattern is an upgraded version of the factory method pattern. The factory method pattern can only produce one level of products, while the abstract factory method pattern can produce multiple levels of products.

The abstract factory method pattern actually contains four classes as the factory method pattern does:

  1. Abstract factory: Declares a set of methods to create a product, one for each product.
  2. Concrete factory: A product method that implements an abstract factory definition, constituting a product category.
  3. Abstract products: Abstract products that declare each product interface.
  4. Specific products: specific products, defining specific product objects produced by specific factories.

The difference is that there are more abstract factory methods and more abstract products in the abstract factory method pattern. In addition, the abstract factory method mode satisfies the following conditions: there are multiple product families, and each specific factory creates the same family of products but belongs to different grade structure; Consume only one of the products of a certain family and use them together.

// Computer product abstract class
public abstart class Computer{
    public abstract void bootUp(a);
}

// Dell series computer products
public class Dell extends Computer{
    @Override
    public void bootUp(a){... }}// Asus
public class Asus extends Computer{
    @Override
    public void bootUp(a){... }}// Memory abstract class
public abstart class Raw{
    public abstract void run(a);
}
// Dell memory products
public class DellRaw extends Raw{
    @Override
    public void run(a){... }}// Asus memory product
public class AsusRaw extends Raw{
    @Override
    public void run(a){... }}// CPU abstract class
public abstart class CPU{
    public abstract void run(a);
}
/ / Intel products
public class Intel extends CPU{
    @Override
    public void run(a){... }}/ / AMD products
public class AMD extends CPU{
    @Override
    public void run(a){... }}// The image factory can produce computers, CPUS, memory
public abstart class ComputerFactory{
    public abstract Computer createComputer(a);
    public abstract CPU createCPU(a);
    public abstract Raw createRaw(a);
}

// Asus factory can produce Asus series products
public AsusFactory extends ComputerFactory{
    @Override
    public Computer createComputer(a){
        Asus asus = new Asus();
        return asus;
    } 
    
    @Override
    public CPU createCPU(a){
        return new AMD();
    } 
    
    @Override
    public Raw createRaw(a){
        return newAsusRaw(); }}// Dell factory can produce Dell series products
public DellFactory extends ComputerFactory{
    @Override
    public Computer createComputer(a){
        return new Dell();
    }
    @Override
    public CPU createCPU(a){
        return new Intel();
    } 
    
    @Override
    public Raw createRaw(a){
        return newDellRaw(); }}Copy the code

The above code is a feature of the abstract factory, if one day the factory becomes a joint venture factory, producing assembly machines, using Intell for CPUS, Asus for computers, and Asus for Raw. This is certainly allowed, and new factories can be created to assemble the products they wish to produce.

conclusion

Abstract factory method pattern has the characteristics of factory method pattern, but also has the following advantages: 1. The abstract Factory method pattern creates a new factory only when it creates a new product of a new type. The factory method pattern can only produce one product, while the abstract factory method pattern can create multiple concrete products of one type. 2. Enhanced program scalability, new product family, do not need to modify the source code.

However, the disadvantage is that when a new product is added to the product family, all existing subfactory classes need to be modified to implement the method of instantiating the new product, which increases the abstractness and difficulty of understanding the system. Therefore, it is necessary to carefully design the original product form before starting to use the abstract factory method pattern to avoid the possibility of adding new products at a later stage.

reference

  • Android Source Code Design Patterns
  • Abstract Factory Pattern (Full version)