Today I’m going to share with you the abstract Factory pattern from the factory design pattern family.

Introduction to the

Anyone familiar with the factory pattern knows that the factory pattern is an excellent solution for creating a certain, class, or group of objects in large numbers. Abstract factory model introduced in this paper is a kind of embodiment of factory model, which belongs to the construction model.

Abstract Factory pattern has four roles: abstract product role, concrete product role, abstract factory role, concrete factory role.

In fact, “abstract factory mode” is very easy to understand, I will give you a small example, you good readers should be able to understand, remember to look at the above four roles in the substitution, so that you understand and remember.

A boss, who wants to produce cars (by cars, there is no specific type, just a general direction, it corresponds to “abstract product roles”).

At the beginning, it must be small scale, so it chooses to produce domestic cars, such as domestic cars and SVU (the proposed car is a specific product, which corresponds to the “specific product role”). At this time, a factory is ok.

When the owner, after a period of operation, makes money and wants to expand, he is not satisfied with producing only “domestic cars”, but also wants to produce “joint venture cars”. At this time, he needs to open another factory. With the continuous development of the industry, there will definitely be other cars involved in the future, such as electricity, oil… Therefore, as a boss, in order to facilitate management, it is necessary to set up a “head office” (the head office corresponds to the “abstract factory role”) and develop a set of norms for other subsidiaries (the subsidiary corresponds to the “concrete factory role”) to follow. However, the parent company does not need to know how a specific subsidiary produces a car.

When we apply such a set of patterns to programming, we form the “abstract factory pattern”.

code

You may not be able to understand this clearly by simply describing it in words, but let’s translate the words into code to deepen your understanding of the “abstract factory pattern”.

The code for “Abstract Product roles” :

/** * Vehicle abstract class */
public abstract class Vehicle {

  /** * The abstract way to produce cars */
  public abstract void production(a);
}
Copy the code

The code for “specific product role” :

/**
 * 国产轿车
 */
public class DomesticCar extends Vehicle {

  @Override
  public void production(a) {
    System.out.println("To produce a domestic car!"); }}/** * Domestic SUV */
public class DomesticSUV extends Vehicle {

  @Override
  public void production(a) {
    System.out.println("Make a domestic SUV!"); }}/** ** ** /
public class DomesticTruck extends Vehicle {

  @Override
  public void production(a) {
    System.out.println("Make a homemade truck!"); }}/** * Joint venture car */
public class JointVentureCar extends Vehicle {

  @Override
  public void production(a) {
    System.out.println("Make a joint venture car!"); }}/** * Joint venture SUV */
public class JointVentureSUV extends Vehicle {

  @Override
  public void production(a) {
    System.out.println("Make a joint venture SUV!"); }}/** * joint venture truck */
public class JointVentureTruck extends Vehicle {

  @Override
  public void production(a) {
    System.out.println("Produce a joint venture truck!"); }}Copy the code

The code for “Abstract Factory role” :

/** * Abstract factory interface */
public interface AbsFactory {

  // A method to create an instance of a vehicle subclass.
  Vehicle createVehicle(String vehicleType);

}
Copy the code

The code for “specific factory role” :

/** ** Factory that produces domestic cars */
public class DomesticFactory implements AbsFactory {

  @Override
  public Vehicle createVehicle(String vehicleType) {
    Vehicle vehicle = null;
    switch (vehicleType) {
      case "car":
        vehicle = new DomesticCar();
        break;
      case "suv":
        vehicle = new DomesticSUV();
        break;
      case "truck":
        vehicle = new DomesticTruck();
        break;
    }
    returnvehicle; }}/** * Factory producing jv car */
public class JointVentureFactory implements AbsFactory {

  @Override
  public Vehicle createVehicle(String vehicleType) {
    Vehicle vehicle = null;
    switch (vehicleType) {
      case "car":
        vehicle = new JointVentureCar();
        break;
      case "suv":
        vehicle = new JointVentureSUV();
        break;
      case "truck":
        vehicle = new JointVentureTruck();
        break;
    }
    returnvehicle; }}Copy the code

The test class:

public class Demo {

  public static void main(String[] args) {
    AbsFactory domesticFactory = new DomesticFactory();
    domesticFactory.createVehicle("car").production();
    JointVentureFactory jointVentureFactory = new JointVentureFactory();
    jointVentureFactory.createVehicle("car").production(); }}Copy the code

conclusion

Advantages:

Instantiated objects do not need to be created separately, but can be obtained through the factory class, greatly improving code reuse. And you don’t have to worry about the process of creating the object, just focus on the result, which also improves software encapsulation.

Disadvantages:

The abstract factory pattern is not conducive to software expansion, and many classes need to be modified each time a new product is added.

This is the end of today’s sharing, if you feel that the article written by “rookie” is still good, remember to like and pay attention to yo! Your support is what keeps me going. Article where to write problems also hope that you can point out, I will be modestly taught.