Simple factory, factory method, abstract factory of Java creational design pattern

Among the design patterns in Java 23, there is a kind of creation pattern, which is usually used to abstract and encapsulate the creation of objects. This kind of creation pattern allows the client caller to not care about how the object is created. Create a corresponding object for an object called a "factory". In this way, the client is not tightly coupled with the object to be created, and the degree of coupling is reduced.

1. Simple factory

The simple factory specifically does not belong to one of the 23 design patterns in Java. It is just a simple package for creating objects. You only need to pass in a correct parameter to get the object you need. Simple factory is more suitable for those occasions where there are fewer classes to create, and the client only knows the parameters passed into the factory class, and does not care about how to create objects.

Let's take a look at an example of a simple factory:

 Our example scenario here is that we need to develop an online learning system platform at this time, which can learn Java, Python, algorithms, front-end courses, etc., for each course we have to record the corresponding video, each course recording step Not the same, so at this time we can use a simple factory to create different course objects to call the method of recording video.

First of all, we have to define a Video abstract class to abstract the Video object. Every course object inherits from the Video object.

Create different course objects

Create a simple factory class, this class is responsible for the creation of different course objects, the create method inside creates corresponding objects according to different parameters

The client can directly create an instance of the simple factory, and then pass in the parameters of the object you want to create, it can help you create the object you want.

UML class diagram in Idea:

Summary of the simple factory: The simple factory encapsulates the process of object creation, so that our caller does not directly depend on the object that needs to be created, and we don't need to care about how the object is created. We only need to tell the factory class which object we need, so The factory class can be created for us and returned to us. But the shortcomings of simple factories are also obvious, that is, when we need to create more and more objects, there will be more and more if branches of the create method in the factory class, which will make the code less and less readable. This also violates the opening and closing principle in the design principle, so the simple factory is suitable for occasions where there are fewer objects to be created.

2. Factory method

The factory method is one of Java's 23 design patterns. We can know that the disadvantage of the simple method is that when we need to create more and more objects, the responsibilities of the factory class will become heavier and heavier, so can we divide into different factory methods for different product levels to create different The object of Nepal?

We can create different course video objects corresponding to different factory classes, so that they don’t exist in simple factories. As the number of objects created increases, the responsibilities of the factory class are getting heavier and heavier, as well as modifying the original factory class to create objects. The logic code is now in compliance with the opening and closing principle. For example, here we want to add a new front-end course, then using the factory method pattern we only need to create a new factory class of front-end course video to be responsible for the creation of the front-end course video object. 

Create a factory abstract class, the abstract methods inside are used to create video objects

Create factory objects for different course videos

Client call, so when we want to create a Java course video object, we only need to create a Java course video factory object, and if we want to add a front-end course, we inherit the VideoFactory object to create The front-end course video factory class is used to create front-end course video objects in this factory.

UML class diagram in Idea:

Summary of the factory method: using the factory method can solve the problem that the simple factory needs to create more and more objects, which leads to continuous modification of the object creation logic in the factory class, thus violating the opening and closing principle. When we need to add a new class to our needs, we don’t need to modify the original code, just add a corresponding factory class to be responsible for the creation of this new class, which is convenient to carry out on the basis of the original code. Expansion meets the requirements of the opening and closing principle.

3. Abstract factory

The abstract factory and the factory method are actually very similar, but for the factory of the abstract factory model, each factory is responsible for the creation of each product family. When it comes to product family, there will be a product level. We want to correspond to the concept. For example, in the e-commerce system, we want to put Midea air conditioners, Midea refrigerators, Haier air conditioners, and Haier refrigerators. For the brand of Midea, Midea air conditioners and Midea refrigerators are equivalent. For a product family, they all belong to the United States. Similarly, Haier's air conditioners and Haier refrigerators are also a product family, and they all belong to Haier. Midea refrigerators and Haier refrigerators belong to the same product level (both refrigerators). Therefore, product families and product levels are categorized from different perspectives. The product families are all different types of products but belong to the same series, and the product levels are the same types in different series. Then the abstract factory creates product family objects for the product family.

At this time, our system has an additional requirement that every course must have a video + notes to be regarded as a complete course. At this time, each course only has a video, so we have to create a course notes class.

The corresponding factory class should have course video objects and course notes objects, because for the Java course, Java videos and Java notes are a set of product families.

Create abstract factory class

Create a product family factory class for Java courses

Client call

Abstract factory UML class diagram:

Summary of Abstract Factory: Abstract factory is for product family, while factory method is for product level. This is the biggest difference between the two. The abstract factory can unify a series of product families into a factory class to create, so that the specific product family is isolated in the application layer code, without worrying about the creation details. But the shortcomings of the abstract factory are also obvious. When we want to expand the product family, we have to modify the abstract methods in the abstract factory. This violates the principle of opening and closing, so it is not all Design patterns are strictly abided by design principles. If our requirements do not change frequently, then the advantages of using the abstract factory pattern outweigh the disadvantages.

 

Guess you like

Origin blog.csdn.net/weixin_37689658/article/details/94362216