Abstract factory pattern of Java design pattern
Abstract factory pattern (abstract factory pattern) is to create other factories around a super factory. This super factory is also known as the factory of other factories. This type of design pattern belongs to the creation pattern, which provides the best way to create objects. In the abstract factory pattern, the interface is the factory responsible for creating a related object, and there is no need to explicitly specify their classes. Each generation All factories can provide objects according to the factory mode.
introduce
intention
Provides an interface to create a series of related or interdependent objects without specifying their specific classes.
Main solution
It mainly solves 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
Define multiple products in a product family.
critical code
Aggregate multiple similar products in one factory.
Application examples
Work, in order to attend some parties, there must be two or more sets of clothes, For example, there are 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 and fashion men's clothes, which are also complete sets, that is, a series of specific products. Suppose one situation (it doesn't exist in reality, otherwise, it can't enter communism, but it helps to explain the abstract factory model). In your home, a wardrobe (specific factory) can only store one kind of such clothes (complete sets, a series of specific products). Each time you take this complete set of clothes, you naturally take it out of the wardrobe. Using OOP ideas, all wardrobe (specific factories) are one of the wardrobe (Abstract factories), and each complete set of clothes includes a specific coat (a specific product) and pants (a specific product), these specific jackets are actually jackets (Abstract products), and the specific pants are pants (another abstract product).
advantage
When multiple objects in a product family are designed to work together, it can ensure that the client always uses only the objects in the same product family.
shortcoming
Product family expansion is very difficult. To add a series of products, you should add code in both the abstract creator and the concrete creator.
Usage scenario
1. QQ for skin, a complete set of change together. 2. Generate programs for different operating systems.
matters needing attention
Product family is difficult to expand, and product level is easy to expand.
realization
We will create shape and color interfaces and entity classes that implement these interfaces. The next step is to create the abstract factory class AbstractFactory. Then define factory classes shapefactory and colorfactory, both of which extend AbstractFactory. Then create a factory creator / generator class factoryproducer. Abstractfactorypatterndemo, our presentation class uses factoryproducer to obtain AbstractFactory objects. It will pass the shape information shape (circle / rectangle / square) to AbstractFactory to get the type of object it needs. At the same time, it will also pass 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();
}
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.");
}
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
Step 3
Create an interface for the color
public interface Color {
void fill();
}
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.");
}
}
public class Green implements Color {
@Override
public void fill() {
System.out.println("Inside Green::fill() method.");
}
}
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 factories.
public abstract class AbstractFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shape) ;
}
Step 6
Create a factory class that extends AbstractFactory and generate the 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;
}
}
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.
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 obtain AbstractFactory and obtain the object of entity class by passing type information.
public class AbstractFactoryPatternDemo {
public static void main(String[] args) {
//获取形状工厂
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
//获取形状为 Circle 的对象
Shape shape1 = shapeFactory.getShape("CIRCLE");
//调用 Circle 的 draw 方法
shape1.draw();
//获取形状为 Rectangle 的对象
Shape shape2 = shapeFactory.getShape("RECTANGLE");
//调用 Rectangle 的 draw 方法
shape2.draw();
//获取形状为 Square 的对象
Shape shape3 = shapeFactory.getShape("SQUARE");
//调用 Square 的 draw 方法
shape3.draw();
//获取颜色工厂
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
//获取颜色为 Red 的对象
Color color1 = colorFactory.getColor("RED");
//调用 Red 的 fill 方法
color1.fill();
//获取颜色为 Green 的对象
Color color2 = colorFactory.getColor("Green");
//调用 Green 的 fill 方法
color2.fill();
//获取颜色为 Blue 的对象
Color color3 = colorFactory.getColor("BLUE");
//调用 Blue 的 fill 方法
color3.fill();
}
}
Step 9
Execute the program and output the results:
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside Red::fill() method.
Inside Green::fill() method.
Inside Blue::fill() method.