[Design Pattern] 1. Simple Factory Pattern Simple Factory

Simple Factory Pattern belongs toCreative Pattern, but it is not one of the 23 design patterns.

Definition: Define a factory class that can return instances of different classes based on different parameters. The created instances usually have a common parent class.

Used in simple factory pattern to create instancesmethodsUsually static method, so the simple factory pattern is also called Static Factory Method

Whatever product is needed, just pass in the parameters corresponding to the product, and you can obtain the required product object without knowing its implementation process.

For example: There is a dumpling shop. When a customer needs a certain kind of dumplings, the dumpling shop generates the corresponding dumplings for the customer. Here you can think of the dumpling shop as a factory, the dumplings as products, and the names of the dumplings as parameters. The dumpling shop returns different dumplings based on different parameters.

For example: the customer wants leek dumplings, and the non-vegetable here is the parameter. The dumpling shop will return the leek dumplings based on the parameter leek (of course, the dumpling shop has the premise of having leek dumplings)

Three types of roles:
Factory (core):Responsible for implementing the internal logic of creating all products. The factory class can be directly called by the outside world to create Required object. → static static method call: class name. static method name () 

Abstract product: The parent class of all objects created by the factory class encapsulates the public methods of the product object, and all specific products are its subclass objects. → abstract
 
Concrete product:The creation goal of the simple factory pattern, all created objects are instances of a specific class. It implements the abstract methods declared in the abstract product.

Class Diagram:

/**

    简单工厂模式
/*

// SimpleFactory 简单工厂类 
public class SimpleFactory{
	public static void main(String[] args){

        // 具体产品   =   类名.静态方法名() → 返回实例化的产品
		Product productA = Factory.createProduct("A")
		productA.info(); // 具体产品的方法

        // 具体产品   =   类名.静态方法名() → 返回实例化的产品
		Product productB = Factory.createProduct("B") 
		productB.info(); // 具体产品的方法
	}
}
 
// Product 抽象产品类,不能被实例化,继承它的子类可以实例化 
abtract class Product{
    // info 抽象方法 
	public abstract void info();
}

// ProductA 具体产品类
class ProductA extends Product{

    // 实现抽象产品类中的抽象方法
	@Override
	public void info(){
		System.out.println("产品A")
	
	}
}

// ProductB 具体产品类
class ProductB extends Product{

    // 实现抽象产品类中的抽象方法
	@Override
	public void info(){
		System.out.println("产品B")
	}
}

// Factory 工厂类
class Factory{
    
    // createProduct 创建产品的静态方法,返回 Product 类型结果
	public static Product createProduct(String type){
        
        // 定义一个产品
		Product product = null;

		switch(type){
			case "A":
				product = new ProductA(); // 实例化具体产品
				break;
			case "B":
				product = new ProductB(); // 实例化具体产品
				break;
			default:
				System.out.println("没有"+type+"类型的产品")
			break;
		}
		return product;
	}
}


Guess you like

Origin blog.csdn.net/weixin_41989013/article/details/134223079