Detailed factory pattern (3)-abstract factory pattern

1. Abstract factory pattern

The abstract factory pattern (Abastract Factory Pattern) is to provide an interface to create a series of related or interdependent objects without specifying their specific classes. The client (application layer) does not depend on the details of how product class instances are created and implemented. It emphasizes that a series of related product objects (belonging to the same product family) are used together to create objects that require a lot of repetitive code. Need to provide a product class library, all products appear with the same interface, so that the client does not depend on the specific implementation.

2. Product hierarchy and product family

Insert picture description here

It can be seen from the above figure that there are three shapes: square, circle and diamond. The same color shade represents the same product family, and the same shape represents the same product level structure. You can also take an example from daily life. For example, Midea produces a variety of household appliances. In the above picture, the darkest square represents the beautiful washing machine, the darkest circle represents the beautiful air conditioner, the darkest diamond represents the beautiful water heater, and the darkest row belongs to the brand of Midea, which is the product family of Midea appliances. Look at the rhombus on the far right, the darkest color we specify is the water heater that represents beauty, then the lighter diamond on the second row represents the Hisense water heater. Similarly, there are Gree water heaters, Gree air conditioners, and Gree washing machines under the same product structure.

Looking at the picture below, the small house on the far left we think is the specific factory, including the Midea factory, the Hisense factory, and the Gree factory. The factories of each brand produce washing machines, water heaters and air conditioners.

Insert picture description here

Through the comparison and understanding of the above two pictures, I believe that everyone has a very vivid understanding of the abstract factory. Next we look at a specific business scenario and implement it in code.

3. Code implementation

Taking courses as an example, each course must provide not only the recording and broadcasting video of the course, but also the classroom notes of the teacher. It is equivalent to changing the current business to the same course. It is not just a course information, but also includes recording and broadcasting videos, classroom notes and even source code to form a complete course. Add two products IVideo recording video and INote classroom notes to the product level.

1) IVideo interface:
public interface IVideo {
    
    
	void record();
}
2) INote interface
public interface INote {
    
    
	void edit();
}
3) Create an abstract factory CourseFactory class
import com.cc.vip.pattern.factory.INote;
import com.cc.vip.pattern.factory.IVideo;
/**
* 抽象工厂是用户的主入口
* 在 Spring 中应用得最为广泛的一种设计模式
* 易于扩展
* Created by Tom. */
public interface CourseFactory {
    
    
	INote createNote();
	IVideo createVideo();
}
4) Create Java product family, Java Video JavaVideo class:
public class JavaVideo implements IVideo {
    
    
	public void record() {
    
    
		System.out.println("录制 Java 视频");
	}
}
5) Extended product level Java class notes JavaNote class:
public class JavaNote implements INote {
    
    
	public void edit() {
    
    
		System.out.println("编写 Java 笔记");
	}
}
6) Create a specific factory JavaCourseFactory for the Java product family:
public class JavaCourseFactory implements CourseFactory {
    
    
	public INote createNote() {
    
    
		return new JavaNote();
}
	public IVideo createVideo() {
    
    
		return new JavaVideo();
	}
}
7) Create a Python product, Python video PythonVideo class:
public class PythonVideo implements IVideo {
    
    
	public void record() {
    
    
		System.out.println("录制 Python 视频");
	}
}
8) Expand the product level Python class notes PythonNote class
public class PythonNote implements INote {
    
    
	public void edit() {
    
    
		System.out.println("编写 Python 笔记");
	}
}
9) Create a specific factory PythonCourseFactory for the Python product family:
public class PythonCourseFactory implements CourseFactory {
    
    
	public INote createNote() {
    
     
		return new PythonNote();
	}
	public IVideo createVideo() {
    
    
		return new PythonVideo();
	}
}
10) Client call:
public static void main(String[] args) {
    
    
	JavaCourseFactory factory = new JavaCourseFactory();
	factory.createNote().edit();
	factory.createVideo().record();
}

4. Summary:

The above code completely describes the two product family Java courses and Python courses, as well as two product grade videos and notes. The abstract factory describes such a complex relationship perfectly and clearly. However, I don't know if you have discovered that if we continue to expand the product level and add the source code Source to the course, then my code must be adjusted from the abstract factory to the specific factory, which obviously does not conform to the principle of opening and closing. Therefore, abstract factories also have disadvantages:

1. It specifies all the product sets that may be created. It is difficult to extend new product levels in the product family, and the interface of the abstract factory needs to be modified.
2. Increased the abstraction and difficulty of understanding the system.

But in practical applications, we must not commit obsessive-compulsive disorder or even cleanliness. It is very normal to upgrade the product hierarchy in actual demand. We can follow the actual situation, as long as the upgrade is not frequent, we can not follow the opening and closing principle. Why not upgrade the code every six months or every year?

Guess you like

Origin blog.csdn.net/weixin_46822085/article/details/108867386