Factory pattern of design pattern - simple factory

simple factory

definition:

A class that creates objects is defined, and this class encapsulates the behavior of instantiating objects.
Explain with the following code

Code

No matter what kind of pizza (Pizza) has to go through steps such as preparing materials, baking, cutting and packaging,
these behaviors are extracted separately as abstract methods

Different types of pizza may have their own unique things, but they are all pizza

/**
	Pizza 对象
 * @author Myli
 * @create 2023-03-08 15:03
 */
public abstract class Pizza {
    
    
    //公共的业务方法
    //准备材料
    public void prepare(){
    
    
        System.out.println("prepare");
    };
     //烤
    public void bake(){
    
    
        System.out.println("bake");
    };
    //切
    public void cut(){
    
    
        System.out.println("cut");
    };
    //打包
    public void box(){
    
    
        System.out.println("box");
    };
}

/**具体抽象产品类  胡椒披萨 必须有一些自己的东西比如胡椒准备*/
public class PepperPizza extends Pizza{
    
    
    //实现业务方法
}
/**具体抽象产品类 希腊比萨 必须有一些自己的东西比如…准备*/
public class GreekPizza extends Pizza{
    
    
    //实现业务方法
 }
 /** 具体抽象产品类 奶酪披萨 必须有一些自己的东西比如奶酪准备 */
public class CheesePizza extends Pizza {
    
    
    //实现业务方法
}
//工厂类  进行代加工
public class SimplePizzaFactory {
    
    
       public Pizza CreatePizza(String ordertype) {
    
    
              Pizza pizza = null;//
              if (ordertype.equals("cheese")) {
    
    
                          pizza = new CheesePizza();
              } else if (ordertype.equals("greek")) {
    
    
                        pizza = new GreekPizza();
              } else if (ordertype.equals("pepper")) {
    
    
                        pizza = new PepperPizza();
              }
                pizza.prepare();
              pizza.bake();
              pizza.cut();
              pizza.box();
              return pizza;
       }
}

When we created this pepperPizza, all his work (preparation, baking, cutting, packing his own unique, etc.) was done

//具体使用,或者在其他地方用这个东西
public class SimpleFactoryTest {
    
    

    public static void main(String[] args) {
    
    
        SimplePizzaFactory simplePizzaFactory = new SimplePizzaFactory();
        //当我们创建这个pepperPizza时,他的自己的独有的和披萨和共有的工作就已经做好了
        Pizza pepper= simplePizzaFactory.CreatePizza("pepper");
        }   
    }

shortcoming:

The creation of classes depends on the factory class, that is to say, if you want to expand the program, you must modify the factory class, which violates the principle of opening and closing. Therefore, from a design perspective, there are certain problems, how to solve them? (To take the example in the article, you must go through SimplePizzaFactory to create a pizza. If you want to make a new type of pizza, such as mango pizza, you not only have to write a mango pizza class that inherits from pizza, but you also have to modify SimplePizzaFactory. Write it and add it to elseif)

in conclusion:

We can define an abstract method for creating objects and create multiple different factory classes to implement the abstract method, so that once new functions need to be added, we can directly add a new factory class without modifying the previous code.
This method is the factory method pattern we are going to talk about next.

Guess you like

Origin blog.csdn.net/m0_54765221/article/details/129405398