Java single application - Architecture Pattern - 03. Design pattern - 02. Abstract factory pattern

Original address: http://www.work100.net/training/monolithic-architecture-design-patterns-abstract-factory-pattern.html
More tutorials: Beam cloud - free course

Abstract factory pattern

Serial number Chapter in text video
1 Summary
2 Realization

Please refer to the navigation above for reading

1. overview

Abstract Factory Pattern is to create other factories around one super factory. This super factory is also called the factory of other factories.
This type of design pattern is a creation pattern, which provides the best way to create objects.

In the abstract factory pattern, an interface is the factory that is responsible for creating a related object, and there is no need to explicitly specify their classes. Each generated factory can provide objects according to the factory pattern.

Intention:

Provides an interface to create a series of related or interdependent objects without specifying their specific classes.

Main solution: mainly solve the problem of 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 family.

How to solve:

In a product family, define multiple products.

Key code:

Aggregate several similar products in one factory.

Application example:

After work, in order to attend some parties, there must be two or more sets of clothes, such as business clothes (complete sets, a series of specific products), fashion clothes (complete sets, a series of specific products), and even for a family, there may be business women's clothes, business men's clothes, fashion women's clothes, fashion men's clothes, which are also complete sets, that is, a series of specific products.
Suppose a situation (in reality, it doesn't exist, otherwise, it's impossible to enter communism, but it's helpful to explain the abstract factory mode), in your home, a certain wardrobe (specific factory) can only store a certain kind of such clothes (complete set, a series of specific products), and each time you take this complete set of clothes, you will naturally take it out of this closet.
With the idea of OOP, all the wardrobes (concrete factories) are one of the wardrobes (Abstract factories), and each complete set of clothes includes specific tops (a specific product), pants (a specific product). These specific tops are actually tops (Abstract products), and specific pants are pants (another Abstract product).

Advantage:

When multiple objects in a product family are designed to work together, it can ensure that clients always use only the objects in the same product family.

Disadvantages:

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

Usage scenario:

  • QQ for skin, a whole set together.
  • Generate programs for different operating systems.

matters needing attention:

Product family is difficult to expand and product grade is easy to expand.

2. implementation

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

AbstractFactoryPatternDemo, our demo class uses FactoryProducer to get AbstractFactory objects.
It passes the Shape information Shape (CIRCLE / RECTANGLE / SQUARE) to AbstractFactory to get the type of 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.

Shape.java, the code is as follows:

public interface Shape {
   void draw();
}

Step 2

Create the entity class that implements the interface.

Rectangle.java, the code is as follows:

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

Square.java, the code is as follows:

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

Circle.java, the code is as follows:

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

Step 3

Create an interface for colors.

Color.java, the code is as follows:

public interface Color {
   void fill();
}

Step 4

Create the entity class that implements the interface.

Red.java, the code is as follows:

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

Green.java, the code is as follows:

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

Blue.java, the code is as follows:

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

Step 5

Create abstract classes for Color and Shape objects to get the factory.

AbstractFactory.java, the code is as follows:

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

Step 6

Create the factory class that extends AbstractFactory, and generate the object of entity class based on the given information.

ShapeFactory.java, the code is as follows:

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;
   }
}

ColorFactory.java, the code is as follows:

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;
   }
}

Step 7

Create a factory creator / generator class to get the factory by passing shape or color information.

FactoryProducer.java, the code is as follows:

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;
   }
}

Step 8

Use FactoryProducer to get AbstractFactory, and pass type information to get object of entity class.

AbstractFactoryPatternDemo.java, the code is as follows:

public class AbstractFactoryPatternDemo {
   public static void main(String[] args) {
 
      //Get shape factory
      AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
 
      //Get objects with the shape of Circle
      Shape shape1 = shapeFactory.getShape("CIRCLE");
 
      //Call the draw method of Circle
      shape1.draw();
 
      //Get object with Rectangle shape
      Shape shape2 = shapeFactory.getShape("RECTANGLE");
 
      //Call the draw method of Rectangle
      shape2.draw();
      
      //Get objects with Square shape
      Shape shape3 = shapeFactory.getShape("SQUARE");
 
      //Call Square's draw method
      shape3.draw();
 
      //Get color factory
      AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
 
      //Get objects with Red color
      Color color1 = colorFactory.getColor("RED");
 
      //Call Red's fill method
      color1.fill();
 
      //Get object with color Green
      Color color2 = colorFactory.getColor("Green");
 
      //Call Green's fill method
      color2.fill();
 
      //Get objects with Blue color
      Color color3 = colorFactory.getColor("BLUE");
 
      //Call Blue's fill method
      color3.fill();
   }
}

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.

Last article: Factory mode
Next article: Singleton mode

52 original articles published, 18 praised, 7627 visited
Private letter follow

Tags: Java

Posted on Fri, 31 Jan 2020 20:58:33 -0800 by hmvartak