5.模版方法模式

  模板方法模式:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤 。

              

#include <iostream>
using namespace std;

// Builder(建造工厂)
class Builder
{
public:
	virtual void build_head() = 0;            // head
	virtual void build_body() = 0;            // body
	virtual void build_arms() = 0;            // arm
	virtual void build_legs() = 0;            // leg
	virtual void build_private_part() = 0;    // private part

        // 固定构造步骤
	void Create()
	{
		build_head();
		build_body();
		build_arms();
		build_legs();
		build_private_part();
	}
};

// man
class ManBuilder : public Builder
{
public:
	void build_head()
	{
		cout << "build man head" << endl;
	}

	void build_body()
	{
		cout << "build man body" << endl;
	}

	void build_arms()
	{
		cout << "build man arm" << endl;
	}

	void build_legs()
	{
		cout << "build man leg" << endl;
	}

	void build_private_part()
	{
		cout << "build xiao dd" << endl;
	}
};

// woman
class WomanBuilder : public Builder
{
public:
	void build_head()
	{
		cout << "build woman head" << endl;
	}

	void build_body()
	{
		cout << "build woman body" << endl;
	}

	void build_arms()
	{
		cout << "build woman arm" << endl;
	}

	void build_legs()
	{
		cout << "build woman leg" << endl;
	}

	void build_private_part()
	{
		cout << "build xiao mm" << endl;
	}
};


void test_01()
{
	// build man
	Builder* builder = new ManBuilder;
	builder->Create();

	delete builder;

	cout << "*****************" << endl;

	// build woman
	builder = new WomanBuilder;
	builder->Create();

	delete builder;
}

int main(int argc, char* argv[])
{
	test_01();

	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值