Abstract factory pattern

Abstract factory pattern is an upgraded version of factory method pattern. Factory method pattern only produces one level of products, while abstract factory pattern can produce multiple levels of products. The following conditions are generally met when using the abstract factory pattern. There are multiple product families in the system. Each specific factory creates products of the same family but belonging to different hierarchical structures. The system can only consume one family of products at a time, that is, the products of the same family can be used together. In addition to the advantages of the factory method pattern, the abstract factory pattern has the following main advantages. The multi-level products associated in the product family can be managed together within the class, instead of introducing multiple new classes for management. When a product family is required, the abstract factory can ensure that the client always uses only the product group of the same product. Abstract factory enhances the scalability of the program. When adding a new product family, there is no need to modify the original code to meet the opening and closing principle. The disadvantage is that when a new product needs to be added to the product family, all factory classes need to be modified. It increases the abstraction and understanding difficulty of the system.

Application example: after work, in order to attend some parties, there must be two or more sets of clothes, such as business clothes (complete sets, a series of specific products), fashion clothes (complete sets, a series of specific products), and even for a family, there may be business women's clothes, business men's clothes, fashion women's clothes and fashion men's clothes, which are also complete sets, That is, a series of specific products. Assuming a situation (it doesn't exist in reality, otherwise, it can't enter communism, but it is conducive to explaining the abstract factory model), in your home, a wardrobe (specific factory) can only store a certain kind of such clothes (complete sets, a series of specific products), and each time you take this complete set of clothes, you naturally take it out of this wardrobe. Understand with OOP thought that all wardrobe (specific factory) are one of wardrobe (Abstract Factory), and each complete set of clothes includes specific jacket (a specific product) and pants (a specific product). These specific jackets are actually jackets (Abstract products) and specific pants are pants (another abstract product).

Advantages: when multiple objects in a product family are designed to work together, it can ensure that the client always uses only the objects in the same product family.

Disadvantages: it is very difficult to expand the product family. To add a series of products, you should add code in both the abstract Creator and the concrete Creator.

Usage scenario: 1. QQ changes the skin, and the whole set changes together. 2. Generate programs for different operating systems.

example

Abstract factory class IAppleFactroy

interface AbstractFactory {
    public Product1 newProduct1();
    public Product2 newProduct2();
}

Specific factory: the product generation method is realized.

class ConcreteFactory1 implements AbstractFactory {
    public Product1 newProduct1() {
        System.out.println("Specific factory 1 generation-->Specific products 11...");
        return new ConcreteProduct11();
    }
    public Product2 newProduct2() {
        System.out.println("Specific factory 1 generation-->Specific products 21...");
        return new ConcreteProduct21();
    }
}

Specific factory category

public class AppleFactoryIn2011 implements IAppleFactroy{
  public IComputer createComputer() {
    return new Pad3();
  }
  public IMobile createMobile() {
    return new Phone12p();
  }
}
public class AppleFactoryIn2012 implements IAppleFactroy{
  public IComputer createComputer() {
    return new PadMini();
  }
  public IMobile createMobile() {
    return new Phone11();
  }
}

Abstract product class IMobile IComputer

public interface IMobile {
  void call();
}
public interface IComputer {
  void play();
}

Specific product category

public class Pad3 implements IComputer{
  public void play() {
    System.out.println("I am iPad3.");
  }
}
public class PadMini implements IComputer{
  public void play() {
    System.out.println("I am iPad Mini.");
  }
}
public class Phone12p implements IMobile {
  public void call() {
    System.out.println("I am IPhone 12p.");
  }
}
public class Phone11 implements IMobile {
  public void call() {
    System.out.println("I am IPhone 11.");
  }
}

Keywords: Design Pattern

Added by jprazen on Wed, 19 Jan 2022 17:20:03 +0200