Business ghosts teach you template method patterns

Insert picture description here

Story line

After the opening, Guangjun’s strong publicity and the prime location of the school made Guangjun’s business extremely hot, and his hands almost never stopped.

But, he’s busy and messy. Sometimes he forgets to add lettuce to the burger, sometimes he forgets to add pearls to the milk tea, and sometimes he brushes the sausage twice with sweet and spicy sauce... and
so on. Guangjun was very troubled.

Why not find a helper? First of all, it is not easy to find a skilled person in a short period of time. In addition, if you are an opening bargain, you will not earn much. The main reason is to build a reputation, so you can't go wrong! ! !

How to do? Guangjun is a bit difficult. At this time, his good friend, that is me, heard that he had bought another store, so he wanted to come to cheer him up, just because Guangjun added two meats to me. But I thought: Damn, he really is a good brother! !
As a result, when he was resting at noon, he said that he had no knowledge of this incident, and he told me about it.

I just mentioned it casually: just have fun with the whole assembly line and make a set.

The speaker has no intention, the listener has the intention. Who knows that he actually set up a set, tut tut, it's not much profitable yet, it's costly.

What is this assembly line like?
Insert picture description here

Probably like this:
the assembly line for making burgers: barbecue, toast, lettuce, butter, bread and meat.
If you want to make a chicken thigh burger, put chicken thigh meat; if you want to make a beef burger, put beef...

The production line of milk tea: taking cups (large, medium and small), putting tea, adding milk, feeding 1, feeding 2, feeding 3.
If there are not enough materials, the process still has to go. There are only three upper limits for materials, and the template must be changed.

The assembly line of grilled sausages will not be written, and it will be boring to write.

In this way, Guangjun started busy and orderly work. Facts have proved that this move will play a pivotal role in the future development of his store.

Template method pattern

Now let’s look at the design pattern, template method pattern that we want to learn in this short story.

So what is the template method pattern?
Define the framework of an algorithm in operation, and delay some steps to the subclass, so that the subclass can redefine some specific steps of the algorithm without changing the structure of an algorithm.
Insert picture description here

It may not be very clear to say that,

封装不变部分,拓展可变部分(把认为是不变部分的算法封装到父类实现,而可变部分可以通过继承来继续拓展。如果要在来个新产品,虾堡,知道怎么做吧)
提取公共部分代码,便于维护。想想看,那个run要是放到子类,那要修改会有几倍工作量?
行为由父类控制,子类实现。

Putting it in the chestnuts on top is to decide when and what to do in advance, for example, roast meat first, then bake bread, as for what meat to roast, what bread to bake, whether it is chicken legs, oatmeal bread, beef, sliced ​​bread, then It is uniquely determined by the specific operator (Guangjun).

In this way, everything is so orderly! ! !

Let's simply come to the realization process of Hamburg flow:

Class diagram display

Insert picture description here

Example code demonstration:

#include<iostream>

using namespace std;

class abstracthumber {
    
    
public:
	virtual void bread() = 0;
	virtual void barbecue() = 0;
	virtual void cream() = 0;
	virtual void lettuce() = 0;

	void run()	//将共同的核心算法流程提炼到抽象类
	{
    
    
		this->bread();
		this->barbecue();
		this->cream();
		this->lettuce();
	}
};

//将细节延迟到子类
//这两步便是模板方法模式的精髓
class chicken :public abstracthumber {
    
    
public:
	void bread() {
    
     cout << "鸡腿堡的面包" << endl; }
	void barbecue() {
    
     cout << "鸡腿堡的鸡腿" << endl; }
	void cream() {
    
     cout << "鸡腿堡的奶油" << endl; }
	void lettuce() {
    
     cout << "鸡腿堡的生菜" << endl; }
};

class beef :public abstracthumber {
    
    
public:
	void bread() {
    
     cout << "牛肉堡的面包" << endl; }
	void barbecue() {
    
     cout << "牛肉堡的鸡腿" << endl; }
	void cream() {
    
     cout << "牛肉堡的奶油" << endl; }
	void lettuce() {
    
     cout << "牛肉堡的生菜" << endl; }
};


int main()
{
    
    
	abstracthumber* a = new chicken();
	abstracthumber* b = new beef();

	a->run();
	b->run();
}

my thoughts:

This method is good, decoupling the core algorithm and the details, but the problem of expansion will still exist.
There will be good performance in teamwork and code inheritance applications.


Come here today, the story of Guangjun entrepreneurship is still being written, everyone can follow me

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43762191/article/details/108685521