Java Classroom | The Essence of Catering (Factory Mode of Design Mode)

foreword

This article mainly talks about the factory model , using easy-to-understand cases in the article, so that you can better learn the knowledge points of this chapter and understand the principles, so that you can do it wisely.

1. What is the factory pattern

The factory mode is one of the 23 design modes of the creation mode . It is the simplest object creation management method, and the object is created and returned according to the type passed by the caller. It encapsulates the process of object creation and reduces the degree of coupling between program modules.

2. The factory model in life

1. Shaxian snacks

Jiang Shuai remembers that Sha County has fried rice, fried noodles, wontons, wontons, steamed dumplings, soups and other dishes, but in Sha County, you only need to tell the boss what you want and pay, and the boss will prepare the relevant food For you, you don’t need to understand how the food is made, you just need to pass the requirements to get what you want, which is very similar to the factory pattern of the design pattern.

image.png

2. Golden Arches (McDonald's) and Kaifeng Cuisine (KFC)

McDonald's and KFC are famous fast food restaurants with many foods, such as hamburgers. The burger is provided to us by them, and we only need to tell the restaurant which burger we want and pay for it. How they finish the burger does not require us to think about it, and this is Like factory pattern for design pattern.

image.png

3. Tea restaurant

These tea restaurants are the same as other restaurants. You only need to tell the clerk what you want and pay, and the kitchen will make the food you ordered according to your needs. It is very similar to the factory pattern of the design pattern.

Please add a picture description

3. Realization of factory mode

Next, Jiang Shuai took a tea restaurant as an example and realized it with the factory model. First create an abstract product category food and two concrete product categories milk tea and sandwich

package com.qianfeng.ran
/\*

*   @author:江帅
*
*   抽象产品
*        定义一个所有食物的抽象父类:食物类

\*/
public abstract class Food{
    
    
//定义一个打印当前食物名的方法
void pirntName();
}

/\*

*   具体产品
*        创建一个继承食物类的子类:奶茶类

\*/
class MikeTea extends Food{
    
    
//重写父类的抽象方法
public void printName(){
    
    
System.out.println("奶茶");
}
}

/\*

*   具体产品
*        创建一个继承食物类的子类:三明治类

\*/
class Sandwich extends Food{
    
    
//重写父类的抽象方法
public void printName(){
    
    
System.out.println("三明治");
}
}

Create a specific factory , similar to a tea restaurant

package com.qianfeng.ran
/*
 * @author:江帅
 *	
 * 	具体工厂
 *		定义一个工厂类:茶餐厅
 */
public class TeaRestaurantFactory{
    
    
	//定义一个根据需求返回具体食物对象的静态方法,类似厨房
    public static Food kitchen(String type){
    
    
        //判断传递进来的需求是否为 null ,为 null 则返回 null 对象
    	if(type == null){
    
    
            return null;
        }
        //根据需求返回对应的食物对象
        if("1".equals(type)){
    
     //假设传递"1"为需要奶茶
            //返回奶茶对象
            return new MikeTea();
        }else if("2".equals(type)){
    
    //假设传递"2"为需要三明治
            //返回三明治对象
            return new Sandwich();
        }
    }
}

Finally, call the corresponding static method (kitchen) through the factory class (tea restaurant) and pass the demand (string) to get the object (food)

package com.qianfeng.ran
/*
 * @author:江帅
 *	
 * 	客户端
 */
public class Demo{
    
    
    public static void main(String[] args){
    
    
        //调用工厂类的静态方法,并传递字符串"1"获取奶茶对象
        Food mikeTea = TeaRestaurantFactory.kitchen("1");
        //执行结果:
        //"奶茶"
        System.out.println(mikeTea.printName());
        //调用工厂类的静态方法,并传递字符串"2"获取三明治对象
        Food sandwich = TeaRestaurantFactory.kitchen("1");
        //执行结果:
        //"三明治"
        System.out.println(sandwich.printName());
    }
}

4. Supporting video

If you are not used to reading technical articles, or do not have a good understanding of the technical concepts in the article, you can take a look at the video tutorials selected by Jiang Shuai for you.

For the Java learning video accompanying this article, click me directly

5. Summary

In the factory mode, we only need to pass the requirements to the method of the factory class to obtain the objects we need, thereby reducing the coupling between classes and classes, and we do not need to understand the creation process of classes and objects. You only need to focus on the use of the object.

But the factory model has a disadvantage, that is, every time you add a specific product class, you need to change the method in the factory class, which violates the open-close principle in the design model. Just like when there is one more dish in a tea restaurant, we need to update the menu.

In the next chapter, we will take you to learn the operating mode of the enterprise (the abstract factory mode of the design mode).

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/130964767