“This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Java Abstract Factory pattern

The Abstract Factory Pattern creates other factories around a gigafactory. The gigafactory is also known as the factory of other factories. This type of design pattern is the creation pattern, which provides the best way to create objects.

In the abstract Factory pattern, an interface is a factory responsible for creating a related object, without explicitly specifying their classes. Each generated factory can provide objects according to the factory pattern.

introduce

Intent: Provide an interface for creating a series of related or interdependent objects without specifying their concrete classes.

Main solution: The main solution is interface selection.

When to use: The products of the system have more than one product family, and the system only consumes the products of one of these family.

How to solve it: Define multiple products within a product family.

Key code: the aggregation of multiple similar products in a factory.

Application example: work, in order to attend some parties, there must be two or more sets of clothes, such as a business attire (complete, a series of specific products), fashionable outfit (complete, a series of specific products), even for a family, there may be business women’s clothing, men’s clothing, fashion women’s clothing, fashion men’s wear, these are complete, namely a series of specific products. Suppose a situation (does not exist in reality, otherwise, can not enter the communist, but beneficial to explain the abstract factory pattern), in your home, a wardrobe (factory) only one such clothes (complete, a series of specific products), every time with this set of clothes is natural to take out from the chest. With the idea of OOP to understand, all chest (factory) is chest (abstract factory) one of a class, and each set of clothes, including specific jacket (a specific products), pants (a specific products), these specific coat also are actually coat (abstract), specific pants also are pants (another abstract products).

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

Disadvantages: Product family extension is very difficult. To add a product in a series, you need to add code in both the abstract Creator and the concrete one.

Use scenario: 1, QQ change skin, a set of change together. 2. Generate programs for different operating systems.

Note: Product family is difficult to expand, product level is easy to expand.

implementation

We will create Shape and Color interfaces and entity classes that implement them. The next step is to create the AbstractFactory class AbstractFactory. Next, we define factory classes ShapeFactory and ColorFactory, both of which extend AbstractFactory. Then create a FactoryProducer/generator class FactoryProducer.

AbstractFactoryPatternDemo, our demo classes use FactoryProducer to obtain AbstractFactory object. It will pass the Shape information Shape(CIRCLE/RECTANGLE/SQUARE) to AbstractFactory to get the type of the object it needs. It also passes the Color information Color(RED/GREEN/BLUE) to AbstractFactory to get the type of object it needs.

Step 1

Create an interface for the shape.

public interface Shape {
   void draw();
}
Copy the code

Step 2

Create an entity class that implements the interface.

public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); }}Copy the code
public class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); }}Copy the code
public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); }}Copy the code

Step 3

Create an interface for color.

public interface Color {
   void fill();
}
Copy the code

Step 4

Create an entity class that implements the interface.

public class Red implements Color { @Override public void fill() { System.out.println("Inside Red::fill() method."); }}Copy the code
public class Green implements Color {
 
   @Override
   public void fill() {
      System.out.println("Inside Green::fill() method.");
   }
}
Copy the code
public class Blue implements Color { @Override public void fill() { System.out.println("Inside Blue::fill() method."); }}Copy the code

Step 5

Create abstract classes for Color and Shape objects to get factories.

public abstract class AbstractFactory {
   public abstract Color getColor(String color);
   public abstract Shape getShape(String shape) ;
}
Copy the code

Step 6

Create a factory class that extends AbstractFactory to generate an object of the entity class based on the given information.

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; }}Copy the code
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

Step 7

Create a factory creator/generator class to retrieve factories by passing shape or color information.

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

Step 8

AbstractFactory is obtained using FactoryProducer, and objects of the entity class are obtained by passing type information.

Public class AbstractFactoryPatternDemo {public static void main (String [] args) {/ / to get shape the factory AbstractFactory shapeFactory  = FactoryProducer.getFactory("SHAPE"); Shape1 = shapeFactory.getShape(" Circle "); shapeFactory.getShape(" Circle "); // Call Circle's draw method shape1.draw(); Shape2 = shapeFactory.getShape(" Rectangle "); // Call Rectangle's draw method shape2.draw(); Shape3 = shapeFactory.getShape(" Square "); // call Square's draw method shape3.draw(); / / to get the COLOR the factory AbstractFactory colorFactory = FactoryProducer. GetFactory (" COLOR "); Color1 = colorFactory.getColor(" Red "); // Call Red's fill method color1.fill(); Color2 = colorFactory.getColor("Green"); // Call Green's fill method color2.fill(); Color3 = colorFactory.getColor(" Blue "); // Call Blue's fill method color3.fill(); }}Copy the code

Step 9

Execute the program and output the result:

Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside Red::fill() method.
Inside Green::fill() method.
Inside Blue::fill() method.
Copy the code