In Java design pattern – factory mode (2) Factory method mode we know that the factory method mode solves the defects in the simple factory mode, so as to meet the open and closed principle, but the era is progressive, and then there are new problems, can the factory only produce one thing? Most of the factories we see are integrated. Hence the abstract factory pattern.

The old photo

Design Mode series:

  • Java Design pattern – singleton pattern
  • Java Design Pattern – Factory Pattern (1) Simple Factory pattern
  • Java Design Pattern – Factory Pattern (2) Factory method pattern
  • Java Design Pattern – Factory Pattern (3) Abstract Factory pattern
  • Java Design pattern – Builder pattern
  • Java Design pattern – Proxy pattern
  • Java Design pattern – Adapter pattern
  • Java Design pattern – Decorator pattern
  • Java Design pattern – Bridge pattern
  • Java Design pattern – Appearance pattern
  • Java Design pattern – Composite pattern
  • Java Design pattern – Share meta pattern
  • Java Design Pattern – Template method pattern
  • Java Design pattern – Policy pattern
  • Java Design pattern – Chain of Responsibility pattern
  • Java Design Pattern – The Mediator pattern
  • Java Design Pattern – Observer Pattern (publish/subscribe pattern)
  • Continuously updated…

One, foreword

1) Overview:

  1. Abstract Factory Pattern belongs to the creation Pattern of design Pattern and is used to build product family. Abstract factory is the most abstract and generic of all factory patterns. Abstract factory refers to a factory pattern used when there are multiple abstract roles. The abstract factory pattern provides an interface to clients to create product objects in multiple product families without specifying the specifics of the product.
  2. Each form in the factory model is a solution to a certain problem, while the factory method is aimed at multiple product family structures. The abstract factory pattern is aimed at multiple product family structure, a product family has multiple product family.
  3. Abstract factory mode is relative to the factory method mode, that is, the factory method mode is for a product series, while the abstract factory mode is for multiple product series, that is, the factory method mode is for a product series of a factory class, while the abstract factory mode is for multiple product series of a factory class.
  4. If the client need to create some product structure, and the product structure is respectively belong to different categories of products, you can use the abstract factory pattern, the abstract factory pattern in the abstract factory class is responsible for defining interfaces for creating an object, the creation of this series of objects are done by the concrete factory class of the abstract factory.

2) Character Overview:

There are four roles in abstract factory pattern, which are abstract factory role, concrete factory role, abstract product role and concrete product role.

  • Abstract factory: Provides an interface for creating products. It contains multiple methods for creating products, and can create multiple levels of products.
  • Concrete factory: it is mainly to realize a number of abstract methods in the abstract factory to complete the creation of concrete products.
  • Abstract products: The abstract factory pattern has multiple abstract products that define a specification for a product and describe its major features and functions.
  • Concrete product: Implements interfaces defined by abstract product roles and is created by concrete factories in a many-to-one relationship with them.

3) above

Here is an extension of the previous article 👉Java Design Pattern – Factory Pattern (2) in the factory method pattern.

The original question is:

Requirements: Design a coffee shop ordering system.

Design a Coffee class and define its two subclasses (AmericanCoffee and latte); Design a coffee shop (CoffeeStore), coffee shop has the function of ordering coffee.

But now we’re expanding.

Nowadays, the business of coffee shops has changed, not only to produce coffee, but also to produce desserts, such as Tiramisu, matcha mousse, etc. If we follow the factory method mode, we need to define tiramisu, matcha mousse, Tiramisu factory, matcha mousse factory and dessert factory, which is prone to explosion. Latte and Americano are both coffee products. Tiramisu, matcha mousse is also a product grade; Latte and tiramisu are the same product family (i.e., both Italian), americano and matcha mousse are the same product family (i.e., both American).

So this example can be implemented using the abstract factory pattern. The class diagram is as follows:

Two, code implementation

1) Abstract products and concrete products:

The first product:

Coffee (first abstract product class), AmericanCoffee, and LatteCoffee (concrete product class)

public abstract class Coffee {
    public abstract void addMilk(a);
    public abstract void addSugar(a);
    public abstract String getName(a);
}
Copy the code
public class AmericanCoffee extends Coffee {
    @Override
    public void addMilk(a) {  System.out.println("Milk your coffee.");  }

    @Override
    public void addSugar(a) {  System.out.println("Sugar your coffee."); }

    @Override
    public String getName(a) {  return "Americano"; }}Copy the code
public class LatteCoffee extends Coffee {
    @Override
    public void addMilk(a) {    System.out.println("Milk your coffee."); }

    @Override
    public void addSugar(a) {  System.out.println("Sugar your coffee."); }

    @Override
    public String getName(a) {  return "Latte"; }}Copy the code

Second product:

Dessert (second abstract product Dessert) MatchaMousse, Tiramisu (concrete product Dessert)

public abstract class Dessert {
    public abstract void show(a);
}
Copy the code
public class MatchaMousse extends Dessert{
    @Override
    public void show(a) {  System.out.println(Matcha mousse); }}Copy the code
public class Tiramisu extends Dessert{
    @Override
    public void show(a) {    System.out.println("Tiramisu"); }}Copy the code

2) Abstract factory and concrete factory

DessertFactory (Abstract factory) AmericanDessertFactory and (concrete factory)

public interface DessertFactory {

    Coffee createCoffee(a);

    Dessert createDessert(a);
}
Copy the code
public class AmericanDessertFactory implements DessertFactory {

    @Override
    public Coffee createCoffee(a) {   return new AmericanCoffee(); }

    @Override
    public Dessert createDessert(a) {  return newMatchaMousse(); }}Copy the code
public class ItalyDessertFactory implements DessertFactory {

    @Override
    public Coffee createCoffee(a) {   return new LatteCoffee(); }

    @Override
    public Dessert createDessert(a) {    return newTiramisu(); }}Copy the code

3) test

public class Client {
    public static void main(String[] args) {
         // Think something American
	    // AmericanDessertFactory factory = new AmericanDessertFactory();
        // If you want to change the Italian flavor, just change the factory class and the rest of the code does not change
        ItalyDessertFactory factory= newItalyDessertFactory(); Coffee coffee = factory.createCoffee(); Dessert dessert = factory.createDessert(); System.out.println(coffee.getName()); dessert.show(); }}Copy the code

If you want to add the same product family, you only need to add a corresponding factory class and do not need to modify the other classes.

4) Advantages and disadvantages:

In addition to the advantages of the factory method pattern, the abstract factory pattern has the following major advantages.

  • Multiple levels of associated products within a product family can be managed together within a class, rather than having to introduce multiple new classes.
  • When a product family is required, an abstract factory ensures that the client always uses only the product group of the same product.
  • Abstract factories enhance the extensibility of programs, and when a new product family is added, there is no need to modify the original code, meeting the open closed principle.

The downside is that all factory classes need to be modified when a new product needs to be added to the product family. The system is abstract and difficult to understand.

To use the abstract factory pattern, the following conditions are generally met.

  • There are multiple product families in the system, and each specific factory creates the same family of products that belong to a different hierarchical structure.
  • The system can only consume one family of products at a time, that is, products of the same family.

5) Usage Scenarios:

Such as: input method change skin, a whole set of change together. Generate programs for different operating systems.

Three, talk to yourself

I do not know whether the article is useful or useless, just want to do a share. I hope you can enjoy it and have a good harvest here.

Hello, be happy every day. See you in the next article.

This series is still being updated…. I’ll be back for sure. 😁