Notes on Java Design Patterns for Beginners--Simple Factory Pattern


Simple Factory Pattern : Also known as Static Factory Method Pattern.

 

The simple factory pattern is not included in the 23 design patterns of GOF.

However, Yan Hong's "Java and Patterns" gives the definition of the Simple Factory pattern .

It is a form of factory pattern.

 

1. Purpose : The simple factory pattern is to have a factory class that determines which product class instance to create based on the incoming parameters.

 

2. Participants:

Abstract product (Product): defines the interface of the object created by the static factory method createProdcut() of the factory class .

Concrete Product: implements the interface of an abstract product .

Factory class (Creator): Define a static factory method createProdcut(), which returns a specific product instance.

 

3. Structure:


 

 

Use real-world examples to map the definition of the simple factory pattern, as follows:

FAW-Volkswagen (factory category) produced a car (abstract product) called the Audi A6 (concrete product).

 

In fact, in the previous article , Notes on Java Design Patterns for Beginners - Factory Pattern , Examples 1 and 4 should be simple factory patterns. It's just that their factory methods have no incoming parameters and cannot dynamically create different products as needed. A static factory method can only return one product instance.

 

If FAW-Volkswagen (factory type) produces two cars (abstract products), one is called Audi A6 (concrete product) and the other is called Audi A8 (concrete product).

How to implement it in Java code? Example 6 is as follows:

 

Car (abstract product role):

 

/*
 * 无论哪个款汽车,都是汽车 (抽象产品类)
 */
public abstract class Car {
	
	public String name;
	
}

 

 

 Audi A6 (specific product role):

 

/*
 * 奥迪A6,是一汽大众生产的一款汽车 (具体产品类)
 */
public class AudiA6Car extends Car {
	
	public AudiA6Car(){
		this.name = "奥迪A6";
		
	}
	
	public String toString(){
		return "一辆"+ this.name;
	}

}

 

 

 

Audi A8 (specific product role):

 

/*
 * 奥迪A8,是一汽大众生产的一款汽车 (具体产品类)
 */
public class AudiA8Car extends Car {
	
	public AudiA8Car(){
		this.name = "奥迪A8";
		
	}
	
	public String toString(){
		return "一辆"+ this.name;
	}

}

 

 

 

FAW-Volkswagen (factory role): 

 The factory method requires an incoming parameter to determine which car the customer wants to buy.

 

/*
 * 生产汽车的工厂,一汽大众 (工厂类)
 */
public class CarFactory {

	/*
	 * 生产汽车(通过一个静态方法来得到一辆汽车的对象)
	 */
	public static Car manufactureCar(String carName){
		Car car = null;
		if(carName.equals("audiA6")){
			return new AudiA6Car();
			
		}else if(carName.equals("audiA8")){
			return new AudiA8Car();
		}
		
		return null;
	}
}

 

 

  

The class structure of the code is as follows:

 

 

 

Client call:

 

Customer A (client) wants to buy an Audi A6, and customer B (client) wants to buy an Audi A8:

 

/*
 * 购买奥迪的顾客(客户端)
 */
public class Customers {
	
	public static void main(String[] args){

		System.out.println("===========顾客A买奥迪A6===========");
		
//		顾客A想买一辆奥迪A6,那么工厂需要生产奥迪A6
		Car myCarA6 = CarFactory.manufactureCar("audiA6");
		System.out.println("奥迪A6被造好,并且出厂了。");
		
//		顾客A得到了他想要的汽车
		System.out.println("我终于买了"+myCarA6+"。真是太好了!");

		System.out.println("===========顾客B买奥迪A8===========");
		
//		顾客B想买一辆奥迪A8,那么工厂需要生产奥迪A8
		Car myCarA8 = CarFactory.manufactureCar("audiA8");
		System.out.println("奥迪A8被造好,并且出厂了。");
		
//		顾客B得到了他想要的汽车
		System.out.println("我终于买了"+myCarA8+"。真是太好了!");
	}

}

 

operation result:

 

===========顾客A买奥迪A6===========
奥迪A6被造好,并且出厂了。
我终于买了一辆奥迪A6。真是太好了!
===========顾客B买奥迪A8===========
奥迪A8被造好,并且出厂了。
我终于买了一辆奥迪A8。真是太好了!

 

  

 

Advantages of the simple factory pattern:

1. Because the factory class uses a static factory method, there is no need to create an instance (object) of the factory class. The outside world can directly call the static method of the factory class to obtain an instance (object) of a specific product.

 

2. The factory class contains the necessary logical judgment to determine which specific class of objects should be created based on the information given by the outside world. That is to say, it can respond to the different needs of the client and dynamically decide which instance (object) of the product class (these product classes inherit from a parent class or interface) should be created and returned.

 

 

Disadvantages of the simple factory pattern:

1. When a new product is needed, although you only need to add a new specific product category (no need to modify other tea categories), the factory class does not have the logic to provide new products. You must modify the source code and recompile the system. This violates the "open-close principle".

 

For example : In Example 6 above , if a new specific product class AudiA9Car is added, the existing factory class CarFactory code must also be modified and recompiled. This is likely to introduce some unexpected errors and affect the stability of the existing system. .

 

2. Because its factory method uses static methods, and static methods cannot be inherited by subclasses, the factory role cannot form a hierarchical structure based on inheritance.

 

3. When the product class has different interface types, the factory class needs to determine when to create which product . This logic mixes the judgment of timing with the judgment of which specific product . This violates the principle of high cohesion responsibility allocation and is very detrimental to the maintenance and expansion of the system.

  

For example: With the development of automobile factories, they will not only produce one style of car, but also have different models of each style of car. If there is only one factory, it will inevitably be unable to meet production needs, will be overloaded, and will easily suffer from equipment aging and other problems. Moreover, if there is a problem with the production equipment of one car, it may affect the production of other models of cars.

 

How should these shortcomings of the simple factory pattern be solved? Next article: Notes on Java Design Patterns for Beginners - Factory Method (Factory Pattern) Pattern

 

Guess you like

Origin blog.csdn.net/louis_lee7812/article/details/83766272