模板方法模式:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤 。
#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;
}