Simple factory pattern of common design patterns in Java

1. Simple factory model

The simple factory model belongs to the creation model, which is responsible for creating the instantiation of other classes by defining a class specifically, and the created instances usually have a common parent class or interface

Two, structure diagram

Insert picture description here

  • Factory : The factory class, the core of the simple factory pattern, is responsible for implementing the creation of an instance. The method of creating a Product in the factory class can be directly called by the outside world.
  • IProduct : abstract product class, which is responsible for describing the common interface common to all instances
  • Product : The specific product category is the goal of the simple factory model.

Three, code demonstration

Through the example of fruit, first we need an abstract fruit interface

package 简单工厂模式;

public interface Fruit {
    
    
	//	生产水果
	public void get();
}

Then we have to create a specific fruit class (for example: apple and Pear), and let it inherit the fruit interface

public class Pear implements Fruit{
    
    

	public void get() {
    
    
		// TODO Auto-generated method stub
		System.out.println("生产梨子!");
		
	}
}
public class Apple implements Fruit {
    
    

	@Override
	public void get() {
    
    
		// TODO Auto-generated method stub
		System.out.println("生产苹果!");
	}
}

Next, you need to create a fruit factory, which produces fruit by passing in parameters

package 简单工厂模式;

public class FruitFactory {
    
    
//	第一种 :简单
//	public	static Fruit getApple() {
    
    
//		return new Apple();
//		
//	}
//	public static Fruit getPear() {
    
    
//		return new Pear();
//		
//	}
	
//	第二种方式:通用
	
	public static Fruit getFruit(String type) {
    
    
//		返回水果对象
		if("apple".equals(type)) {
    
    
			return new Apple();
		}else if("Pear".equals(type)) {
    
    
			return new Pear();
		}else {
    
    
			return null;
		}
	}
}

Next, call it by creating an entity class!

package 简单工厂模式;
//测试类
public class Text {
    
    
public static void main(String[] args) {
    
    
/**	第三种方式 简单工厂模式
 * 
 * 直接通过工厂去调用实例化对象,
 * 不需要考虑如何后端是如何实现
 */
//	简单型
//	Fruit apple = FruitFactory.getApple();
//	apple.get();
//	Fruit pear = FruitFactory.getApple();
//	apple.get();
	
//	通用型
	Fruit apple = FruitFactory.getFruit("apple");
	apple.get();
	Fruit pear = FruitFactory.getFruit("Pear");
	pear.get();
}
}

Fourth, the advantages and disadvantages of the simple factory model

In this pattern, the factory class is the key to the whole pattern. It contains the necessary logical judgments, which can determine which specific class object should be created based on the information given by the outside world. The user can directly create the required instance based on the factory class when using it, without having to understand how these class objects are created and how they are organized. Conducive to the optimization of the entire software architecture.
It is not difficult to find that the shortcomings of the simple factory pattern are also reflected in its factory class. Since the factory class concentrates the creation logic of all instances, it is not good at == "high cohesion" ==. In addition, when the specific product categories in the system continue to increase, it may appear that the factory categories are required to be modified accordingly.Scalability is not very good

Guess you like

Origin blog.csdn.net/weixin_44676935/article/details/104996481