Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

A simple factory can be compared to: there is a clinker ball factory, which can produce red clinker balls, black clinker balls, and yellow clinker balls. The abstract factory model can be compared to: there is a clinker ball factory, including plastic ball factory, iron ball factory, etc. But it cannot be understood as a subset. It’s more like a mother bed factory

The characteristics of

It is to abstract all the related factories into a unified abstract factory

For example, if a plastic ball needs “paint, plastic”, then creating a plastic ball factory requires a paint factory and a plastic factory

Every element consists of shapes and colors.

Implementation logic

  1. Create an AbstractFactory to get the required factories :ShapeFactory and ColorFactory
  2. Create ShapeFactory and ColorFactory to get the desired objects

Practical scenario

QQ show for skin, a set of change together can also be a change

The implementation code

  1. Create interfaces for shapes and colors to specify the characteristics that all shapes and colors need to follow
public interface Shape{
    void draw(a);
}

public interface Color{
    void fill(a);
}
Copy the code
  1. Create implementation classes for shapes and colors, such as circles and squares; Red, blue
public class Rectangle implements Shape {
 
   @Override
   public void draw(a) {
      System.out.println("Inside Rectangle::draw() method."); }}public class Square implements Shape {
 
   @Override
   public void draw(a) {
      System.out.println("Inside Square::draw() method."); }}public class Red implements Color {
 
   @Override
   public void fill(a) {
      System.out.println("Inside Red::fill() method."); }}public class Blue implements Color {
 
   @Override
   public void fill(a) {
      System.out.println("Inside Blue::fill() method."); }}Copy the code
  1. Create abstract factories for shapes and colors (factories have methods to get colors or get shapes)
public abstract class AbstractFactory {
   public abstract Color getColor(String color);
   public abstract Shape getShape(String shape) ;
}
Copy the code
  1. Create a shape factory and a color factory that inherit the abstract factory and produce shapes and colors
public class ShapeFactory extends AbstractFactory {
    
   @Override
   public Shape getShape(String shapeType){
      if(shapeType == null) {return null;
      }        
      if(shapeType.equalsIgnoreCase("CIRCLE")) {return new Circle();
      } else if(shapeType.equalsIgnoreCase("RECTANGLE")) {return new Rectangle();
      } else if(shapeType.equalsIgnoreCase("SQUARE")) {return new Square();
      }
      return null;
   }
   
   @Override
   public Color getColor(String color) {
      return null; }}public class ColorFactory extends AbstractFactory {
    
   @Override
   public Shape getShape(String shapeType){
      return null;
   }
   
   @Override
   public Color getColor(String color) {
      if(color == null) {return null;
      }        
      if(color.equalsIgnoreCase("RED")) {return new Red();
      } else if(color.equalsIgnoreCase("GREEN")) {return new Green();
      } else if(color.equalsIgnoreCase("BLUE")) {return new Blue();
      }
      return null; }}Copy the code
  1. Create a factory creator
public class FactoryProducer {
   public static AbstractFactory getFactory(String choice){
      if(choice.equalsIgnoreCase("SHAPE")) {return new ShapeFactory();
      } else if(choice.equalsIgnoreCase("COLOR")) {return new ColorFactory();
      }
      return null; }}Copy the code

When using:

      // Get the shape factory
      AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
 
      // Get an object of the shape Circle
      Shape shape1 = shapeFactory.getShape("CIRCLE");
 
      // Call Circle's draw method
      shape1.draw();
      
      // Get the color factory
      AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
 
      // Get the object with the color Red
      Color color1 = colorFactory.getColor("RED");
 
      // Call Red's fill method
      color1.fill();
Copy the code

Personal understanding

From the point of view of code logic, abstract factory is to abstract a series of relational factories to generate an abstract factory class, and then provide a factory creator.

In terms of business logic, a draw factory is to manage a series of objects that may be operated together, so that they can be logically connected or retrieved. Let’s say I want to get

The abstract factory pattern can be used when the objects to be created are a series of interrelated or interdependent product families.

The design pattern is just a recommendation, which means it can be used correctly

  1. Maintenance of latecomers
  2. Team collaboration
  3. The code quality

Is more helpful, but different from some frameworks that can directly increase the amount of reuse and reduce the amount of code.

For learners

  1. Everyone says it’s a good way to write, so I’ll learn
  2. Look at some source code essential medicine
  3. I’m sure you’ll use it in an interview
  4. Essential medicine for advancing from the beginner to the advanced