在学习建造者模式时,一直有一个问题想不明白,就是为什么要存在指挥者类。感觉没有指挥者类也完全同样的简单快捷,甚至比没有指挥者类更容易理解。
以下是本人用C++写的一个例子程序:
//Builder.cpp
//运用了建造者模式
#include <iostream>
using namespace std;
class Automobile//抽象汽车类
{
public:
virtual ~Automobile(){cout<<"destruction automobile"<<endl;}
virtual void BuildWheel()=0; //建造车轮
virtual void BuildBody()=0; //建造车身
virtual void BuildVehicle()=0; //建造车壳
protected:
Automobile(){}
};
class Car:public Automobile
{
public:
virtual void BuildWheel()
{
cout<<"build the wheel of car"<<endl;
}
virtual void BuildBody()
{
cout<<"build the body of car"<<endl;
}
virtual void BuildVehicle()
{
cout<<"build the Vechicle of car"<<endl;