Abstract factory schema definitions

Abstract factory pattern is the most abstract and general of all factory patterns. Abstract Factory pattern is 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.

Here is a new concept, ** product family: ** refers to a family of functionally related products located in different product hierarchy. Usually in the same position in different hierarchical structures. Obviously, the number of products contained in each product family is equal to the number of product grade structure, forming a two-dimensional coordinate system, horizontal coordinate is product grade structure, vertical coordinate is product family. It’s called a phase diagram.


The difference between abstract factory pattern and factory method pattern

The factory method pattern targets a product hierarchy; The abstract factory pattern is aimed at multiple product hierarchies, hence the concept of a product family. In programming, a product structure is usually represented as an interface or abstract class, that is, all products in the factory method pattern come from the same interface or abstract class, while products in the abstract factory pattern come from different interfaces or abstract classes.

The Abstract factory pattern is an updated version of the factory method pattern, which is suitable for multiple businesses, or multiple classifications.

A few days ago I wrote about factory Method Patterns, Design Patterns (2)- Factory Method Patterns in detail.

The article gives an example of a mobile phone factory producing mobile phones. We continue to use this example to analyze these two modes.

Let’s first recall the example of the factory method pattern: A mobile phone factory produces two kinds of huawei and Xiaomi phones. The model is shown as follows:

From the model, it can be found that the two phones are in a hierarchical structure, that is, both implement or integrate the same interface or abstract class.

Also take mobile phones as an example, now add the requirements of mobile phone size, the model is as follows:

The first thing to know is the concept of product family, which I defined at the beginning of this article.

It can be found from the above model:

The 6.0-inch Huawei PHONE A1 and the 6.0-inch Xiaomi phone B1 belong to the same product family. The 5.5-inch Huawei phone A2 and the 5.5-inch Xiaomi phone B2 belong to the same product family. Factory1 means to make a 6.0-inch phone, Factory2 means to make a 5.5-inch phone.

The creation method in the Abstract Factory class in the Abstract Factory pattern is related to the number of product families.

There are N product level structures and there are N implementation factory classes.


Abstract factory pattern code demo

Let’s take the abstract factory example model above:

1. Let’s take a look at the abstract product categories of the two mobile phones:

Public Abstract Class AbstractHUAWEI {// Huawei mobile phone common method, such as brand public voidcommonMethod(){} // Same method, different implementation. Public abstract void dosomething(); }Copy the code
Public abstract class AbstractMI {public abstract class AbstractMI {public abstract class AbstractMIcommonMethod(){} // Same method, different implementation. Public abstract void dosomething(); }Copy the code

2. Corresponding product implementation classes of the two mobile phones

public class HUAWEI_A1 extends AbstractHUAWEI{
   @Override
   public void dosomething() {
       Log.i("qzs"."I'm a 6.0 inch Huawei PHONE A1.");
   }
}

public class HUAWEI_A2 extends AbstractHUAWEI {
   @Override
   public void dosomething() {
       Log.i("qzs"."I am a 5.5-inch Huawei A2");
   }
}

public class MI_B1 extends AbstractMI {
   @Override
   public void dosomething() {
       Log.i("qzs"."I'm a xiaomi B1 with 6.0 inches.");
   }
}

public class MI_B2 extends AbstractMI {
   @Override
   public void dosomething() {
       Log.i("qzs"."I'm a 5.5-inch Xiaomi B1."); }}Copy the code

3. Abstract factory classes

Public abstract class AbstractFactory {public abstract AbstractHUAWEI createSize1(); Public abstract AbstractMI createSize2(); }Copy the code

4. Two factory implementation classes


public class Factory1 extends AbstractFactory {
   @Override
   public AbstractHUAWEI createSize1() {
       return new HUAWEI_A1();
   }

   @Override
   public AbstractMI createSize2() {
       returnnew MI_B1(); }}Copy the code
public class Factory2 extends AbstractFactory {
   @Override
   public AbstractHUAWEI createSize1() {
       return new HUAWEI_A2();
   }

   @Override
   public AbstractMI createSize2() {
       returnnew MI_B2(); }}Copy the code

5. Call

AbstractFactory factory1=new Factory1(); AbstractFactory factory2=new Factory2(); A1 = factory1.createHuawei (); // a1.dosomething(); A2 = factory2.createHuawei (); // a2.dosomething(); AbstractMI B1 = factory1.createmi (); // b1.dosomething(); AbstractMI B2 = factory2.createmi (); // b2.dosomething();Copy the code

As you can see from the calling class, the process of creating a production object is independent of the implementation class.


Advantages and disadvantages of the abstract factory pattern

1. The advantages

  • The concrete class is isolated (as you can see from the code that calls the class) and is not public.

  • It is very convenient to add a specific implementation factory class to the product series, which conforms to the “open and closed principle” (for example, in the above example, I added a 5.0-inch model under the two types of mobile phones, so I can directly add a new specific factory class).

2. The shortcomings

  • It is very difficult to extend the product family of abstract factory mode. Take the example of the mobile phone above, if I add a mobile phone brand OPPO, then the two brands will be changed to three brands, try to change it, you will find: first, I need to change the abstract factory class, and then I will find that all the classes are basically changed. Changing abstract classes and interfaces is a big no-no!

  • The number of class files increases too fast

Welcome to pay attention to my wechat public number: Android Dry Goods Shop