赞
踩
目录
建造者模式(Builder):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式是在当创建复杂对象的算法应该独立于该对象的组成部分以及它们装配方式时适用的模式。
下面给出一个UML实例:
程序运行截图如下:
源码如下:
Head.h
- #include <string>
- using namespace std;
-
- typedef string Graphics;
- typedef string Pen;
-
-
- class PersonBuilder{
-
- public:
- virtual void buildHead(){}
- virtual void buildBody(){}
- virtual void buildArmLeft(){}
- virtual void buildArmRight(){}
- virtual void buildLegLeft(){}
- virtual void buildLegRight(){}
-
- PersonBuilder(Graphics g, Pen p);
- virtual ~PersonBuilder();
-
- private:
- Graphics *g;
- Pen *p;
- };
-
- class PersonThinBuilder : public PersonBuilder{
-
- public:
- PersonThinBuilder(Graphics g, Pen p);
- void buildHead();
- void buildBody();
- void buildArmLeft();
- void buildArmRight();
- void buildLegLeft();
- void buildLegRight();
- };
-
- class PersonFatBuilder : public PersonBuilder{
-
- public:
- PersonFatBuilder(Graphics g, Pen p);
- void buildHead();
- void buildBody();
- void buildArmLeft();
- void buildArmRight();
- void buildLegLeft();
- void buildLegRight();
- };
-
- class PersonDirector{
-
- public:
- PersonDirector(PersonBuilder *pb);
- void createPersion();
-
- private:
- PersonBuilder *pb;
- };
Head.cpp
- #include "Head.h"
- #include <iostream>
-
- PersonThinBuilder::PersonThinBuilder(Graphics g, Pen p):PersonBuilder(g, p)
- {
-
- }
-
- void PersonThinBuilder::buildHead()
- {
- cout << "瘦子 画头" << endl;
- }
-
- void PersonThinBuilder::buildBody()
- {
- cout << "瘦子 画身体" << endl;
- }
-
- void PersonThinBuilder::buildArmLeft()
- {
- cout << "瘦子 画左手" << endl;
- }
-
- void PersonThinBuilder::buildArmRight()
- {
- cout << "瘦子 画右手" << endl;
- }
-
- void PersonThinBuilder::buildLegLeft()
- {
- cout << "瘦子 画左脚" << endl;
- }
-
- void PersonThinBuilder::buildLegRight()
- {
- cout << "瘦子 画右脚" << endl;
- }
-
- PersonBuilder::PersonBuilder(Graphics g, Pen p)
- {
- this->g = new Graphics(g);
- this->p = new Pen(p);
- }
-
- PersonBuilder::~PersonBuilder()
- {
- delete g;
- delete p;
- }
-
- PersonFatBuilder::PersonFatBuilder(Graphics g, Pen p):PersonBuilder(g, p)
- {
-
- }
-
- void PersonFatBuilder::buildHead()
- {
- cout << "胖子 画头" << endl;
- }
-
- void PersonFatBuilder::buildBody()
- {
- cout << "胖子 画身体" << endl;
- }
-
- void PersonFatBuilder::buildArmLeft()
- {
- cout << "胖子 画左手" << endl;
- }
-
- void PersonFatBuilder::buildArmRight()
- {
- cout << "胖子 画右手" << endl;
- }
-
- void PersonFatBuilder::buildLegLeft()
- {
- cout << "胖子 画左脚" << endl;
- }
-
- void PersonFatBuilder::buildLegRight()
- {
- cout << "胖子 画右脚" << endl;
- }
-
- PersonDirector::PersonDirector(PersonBuilder *pb)
- {
- this->pb = pb;
- }
-
- void PersonDirector::createPersion()
- {
- pb->buildBody();
- pb->buildHead();
- pb->buildArmLeft();
- pb->buildArmRight();
- pb->buildLegLeft();
- pb->buildLegRight();
- }
main.cpp
- #include "Head.h"
- #include <iostream>
- using namespace std;
-
- int main(int *argc, int *argv){
-
- PersonThinBuilder *pT = new PersonThinBuilder("画瘦子", "红笔");
- PersonDirector *pD = new PersonDirector(pT);
-
- pD->createPersion();
-
- delete pT;
- delete pD;
-
- cout << "------------ 华 丽 的 分 割 线 ------------" << endl;
-
- PersonFatBuilder *pF = new PersonFatBuilder("画胖子", "黑笔");
- PersonDirector *pD2 = new PersonDirector(pF);
- pD2->createPersion();
-
- delete pF;
- delete pD2;
-
-
- getchar();
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。