现在的位置: 首页 > 综合 > 正文

建造者模式

2018年04月29日 ⁄ 综合 ⁄ 共 1490字 ⁄ 字号 评论关闭
#include <iostream>
using namespace std;

class Car
{
public:
	Car(){
	}
	virtual void buildBody() = 0;
	virtual void buildWheel() = 0;
	virtual void paintColor() = 0;
	virtual void buildEngin() = 0;
	virtual void buildDoor() = 0;
	
	void Show()
	{
		cout << "brand: " << brand << endl
				<< "color: " << color <<" length: " << lenth<<" width: "<<width
				<<" height: "<< height<<" Engin: "<<Engin<<" doorType: "<<doortype
				<<" WheelSize: "<< wheelsize << endl;
	}
	
protected:	
	string brand, color, Engin, doortype;
	int lenth, width, height, wheelsize;
};

class Benz : public Car
{
public:
	Benz(){	
	}	
	virtual void buildBody()
	{
		this->brand = "Benz";
		this->lenth = 5;
		this->width = 3;
		this->height= 2;
	}
	virtual void buildWheel()
	{
		this->wheelsize = 1;
	}
	virtual void paintColor()
	{
		this->color = "white";
	}
	virtual void buildEngin()
	{
		this->Engin = "8缸";
	}
	virtual void buildDoor()
	{
		this->doortype = "防盗";
	}
};

class BMW : public Car
{
public:
	BMW(){	
	}	
	virtual void buildBody()
	{
		this->brand = "BMW";
		this->lenth = 4;
		this->width = 5;
		this->height= 3;
	}
	virtual void buildWheel()
	{
		this->wheelsize = 2;
	}
	virtual void paintColor()
	{
		this->color = "black";
	}
	virtual void buildEngin()
	{
		this->Engin = "12缸";
	}
	virtual void buildDoor()
	{
		this->doortype = "防弹";
	}
};

class Director
{
public:
	Director(Car * Type) : carType(Type){	}
	void CreateCar()
	{
		carType->buildBody();
		carType->buildWheel();
		carType->paintColor();
		carType->buildEngin();
		carType->buildDoor();
	}

private:
	Car *carType;	
};

/*
class CarFactory
{
	
};

class BenzFactory : public CarFactory
{
	
};

class BenzFactory : public CarFactory
{
	
};

class UI
{
	
};*/

int main(void)
{
	Director *drt;
	//Car *bmw_car = new BMW();  //指定车型 
	Car *bmw_car = new Benz();
	drt = new Director(bmw_car);
	drt->CreateCar();
	bmw_car->Show();

	return 0;
}

 

抱歉!评论已关闭.