大话设计模式——建造者模式


 

1.建造者模式(Builder),又叫生成器模式,它是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的比奥斯。如果我们用了建造者模式,那么用户就只需指定需要建造的类型就可以得到它们,而具体建造的过程和细节就不需知道了。

2.建造者模式(Builder)结构图

大话设计模式——建造者模式  

3.建造者模式主要是用于创建一些复杂的对象,这些对象内部构建间的建造顺序通常是稳定的,但对象内部的构建通常面临着复杂的变化。

4.建造者模式的好处是使得建造代码与表示代码分离,由于建造者隐藏了该产品是如何组装的,所以若需要改变一个产品的内部表示,只需要再定义一个具体的建造者就可以了。

 以下为一个例子的代码结构图:

大话设计模式——建造者模式

代码如下:

 

/*PersonBuilder.h*/



#ifndef PERSONBUILDER_H

#define PERSONBUILDER_H



class PersonBuilder

{

public:

	virtual void buildHead() = 0;

	virtual void buildBody() = 0;

	virtual void buildArmLeft() = 0;

	virtual void buildArmRight() = 0;

	virtual void buildLegLeft() = 0;

	virtual void buildLegRight() = 0;

};



#endif

 

/*PersonThinBuilder.h*/



#ifndef PERSONTHINBUILDER

#define PERSONTHINBUILDER



#include"PersonBuilder.h"



class PersonThinBuilder : public PersonBuilder

{

public:

	void buildHead();

	void buildBody();

	void buildArmLeft();

	void buildArmRight();

	void buildLegLeft();

	void buildLegRight();

};



#endif

 

/*PersonThinBuilder.cpp*/

#include<iostream>

#include"PersonThinBuilder.h"

using namespace std;



void PersonThinBuilder::buildHead()

{

	cout<<"This Thin Head is builded..."<<endl;

}



void PersonThinBuilder::buildBody()

{

	cout<<"This Thin Body is builded..."<<endl;

}



void PersonThinBuilder::buildArmLeft()

{

	cout<<"This Thin Left Arm is builded..."<<endl;

}



void PersonThinBuilder::buildArmRight()

{

	cout<<"This Thin Right Arm is builded..."<<endl;

}



void PersonThinBuilder::buildLegLeft()

{

	cout<<"This Thin Left Leg is builded..."<<endl;

}



void PersonThinBuilder::buildLegRight()

{

	cout<<"This THin Right Leg is builded..."<<endl;

}
/*PersonFatBuilder.h*/



#ifndef PERSONFATBUILDER

#define PERSONFATBUILDER



#include"PersonBuilder.h"



class PersonFatBuilder : public PersonBuilder

{

public:

	void buildHead();

	void buildBody();

	void buildArmLeft();

	void buildArmRight();

	void buildLegLeft();

	void buildLegRight();

};



#endif
/*PersonFatBuilder.cpp*/

#include<iostream>

#include"PersonFatBuilder.h"

using namespace std;



void PersonFatBuilder::buildHead()

{

	cout<<"This Fat Head is builded..."<<endl;

}



void PersonFatBuilder::buildBody()

{

	cout<<"This Fat Body is builded..."<<endl;

}



void PersonFatBuilder::buildArmLeft()

{

	cout<<"This Fat Left Arm is builded..."<<endl;

}



void PersonFatBuilder::buildArmRight()

{

	cout<<"This Fat Right Arm is builded..."<<endl;

}



void PersonFatBuilder::buildLegLeft()

{

	cout<<"This Fat Left Leg is builded..."<<endl;

}



void PersonFatBuilder::buildLegRight()

{

	cout<<"This Fat Right Leg is builded..."<<endl;

}
/*PersonDirector.h*/



#ifndef PERSONDIRECTOR

#define PERSONDIRECTOR



#include"PersonBuilder.h"



class PersonDirector

{

private:

	PersonBuilder *per;

public:

	PersonDirector(PersonBuilder *_per);

	void CreatePerson();

};



#endif
/*personDirector.cpp*/

#include"PersonDirector.h"



PersonDirector::PersonDirector(PersonBuilder *_per)

{

	per = _per;

}



void PersonDirector::CreatePerson()

{

	per->buildHead();

	per->buildBody();

	per->buildArmLeft();

	per->buildArmRight();

	per->buildLegLeft();

	per->buildLegRight();

}
/*main.cpp*/

#include<iostream>

#include"PersonDirector.h"

#include"PersonThinBuilder.h"

#include"PersonFatBuilder.h"

using namespace std;



int main()

{

	PersonDirector thindirector(new PersonThinBuilder());

	PersonDirector fatdirector(new PersonFatBuilder());



	thindirector.CreatePerson();

	fatdirector.CreatePerson();

	return 0;

}
This Thin Head is builded...

This Thin Body is builded...

This Thin Left Arm is builded...

This Thin Right Arm is builded...

This Thin Left Leg is builded...

This THin Right Leg is builded...

This Fat Head is builded...

This Fat Body is builded...

This Fat Left Arm is builded...

This Fat Right Arm is builded...

This Fat Left Leg is builded...

This Fat Right Leg is builded...

请按任意键继续. . 

你可能感兴趣的:(建造者模式)