本文主要是介绍大话设计模式8—建造者模式(建造小人),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
大话设计模式8—建造者模式(建造小人)
- 1.需求:建造小人
- 2.普通实现
- 3.建造者模式实现
- 4.建造者模式
1.需求:建造小人
假设需要使用画笔和图片画两个小人,小人只需要包含头部、身体和腿就可以。这两个小人一个是瘦人、一个是胖子。现给出画笔和图片两个类如下:
class Pen {
public:Pen();
};class Graphics {
public:Graphics();
};
构造函数
Pen::Pen() {cout << "use yellow pen" << endl;
}Graphics::Graphics() {cout << "start to write" << endl;
}
main函数
int main()
{Pen *p = new Pen();Graphics *g = new Graphics();//自己添加return 0;
}
请设计BuildThinPerson和BuildFatPerson两个类完成最终需要输出如下。
2.普通实现
设计两个类BuildThinPerson 和BuildFatPerson,这两个类十分相似,都有两个成员变量m_pen和m_graphics指针,来自题设给定的Pen和Graphics 两个类,BuildThinPerson的方法就是画头、身子、腿如下。
class BuildThinPerson {
public:BuildThinPerson(Pen *pen, Graphics *graphics);void BuildHead();void BuildBody();void BuildLeg();void BuildThin();
private:Pen *m_pen;Graphics *m_graphics;
};
BuildThinPerson 类的实现很简单,由于main函数已经实现了一部分,所以BuildThinPerson 类的构造函数传参即可。在BuildHead(); BuildBody();BuildLeg()三个函数中打印需要的输出,而BuildThin()函数负责调用三个函数封装起来。
BuildThinPerson::BuildThinPerson(Pen * pen, Graphics * graphics) :m_pen(pen), m_graphics(graphics) {
}void BuildThinPerson::BuildHead() {cout << "build Head of thin person" << endl;
}void BuildThinPerson::BuildBody() {cout << "build Body of thin person" << endl;
}void BuildThinPerson::BuildLeg() {cout << "build Leg of thin person" << endl;
}void BuildThinPerson::BuildThin()
{BuildHead();BuildBody();BuildLeg();
}
BuildFatPerson类的实现只有打印语句和BuildThinPerson不同略去实现。那么补全的main函数如下
int main()
{Pen *p = new Pen();Graphics *g = new Graphics();BuildThinPerson *thin_person = new BuildThinPerson(p, g);thin_person->BuildThin();BuildFatPerson *fat_person = new BuildFatPerson(p, g);fat_person->BuildFat();return 0;
}
达到建造两个小人的要求
3.建造者模式实现
查看上述代码,可以发现两个类BuildThinPerson 和BuildFatPerson相似度很高,所以第一步,我们考虑把共同的东西放到新设计的父类PersonBuilder中,BuildThinPerson 和BuildFatPerson继承他,三类如下:
class PersonBuilder {
protected:Pen *m_pen;Graphics *m_graphics;
public:PersonBuilder(Pen *pen,Graphics *graphics);virtual ~PersonBuilder() {};virtual void BuildHead() {};virtual void BuildBody() {};virtual void BuildLeg() {};
};class BuildThinPerson:public PersonBuilder{
public:BuildThinPerson(Pen *pen, Graphics *graphics);virtual void BuildHead();virtual void BuildBody();virtual void BuildLeg();
};class BuildFatPerson:public PersonBuilder {
public:BuildFatPerson(Pen *pen, Graphics *graphics);virtual void BuildHead();virtual void BuildBody();virtual void BuildLeg();
};
这三个类实现也很简单,子类重写父类的BuildHead(); BuildBody();BuildLeg()函数。
PersonBuilder::PersonBuilder(Pen* pen, Graphics* graphics) : m_pen(pen), m_graphics(graphics) {
}BuildThinPerson::BuildThinPerson(Pen* pen, Graphics* graphics): PersonBuilder(pen,graphics){
}void BuildThinPerson::BuildHead() {cout << "build Head of thin person" << endl;
}void BuildThinPerson::BuildBody() {cout << "build Body of thin person" << endl;
}void BuildThinPerson::BuildLeg() {cout << "build Leg of thin person" << endl;
}BuildFatPerson::BuildFatPerson(Pen * pen, Graphics * graphics) :PersonBuilder(pen, graphics){
}void BuildFatPerson::BuildHead() {cout << "build Head of fat person" << endl;
}void BuildFatPerson::BuildBody() {cout << "build Body of fat person" << endl;
}void BuildFatPerson::BuildLeg() {cout << "build Leg of fat person" << endl;
}
重点来了,客户端调用时还是需要知道建造头、身子、腿的方法,我们想要隔绝用户和建造过程的关联,不让用户知道建造的过程和细节。所以我们还需要一个指挥者的类来控制建造过程。
指挥者PersonDirector 类管理抽象建造者类的指针,通过调用CreatPerson进行建造。
class PersonDirector {
private:PersonBuilder* m_person_builder;
public:PersonDirector(PersonBuilder* person_builder);void CreatPerson();
};
PersonDirector类实现,构造函数通过用户告诉指挥者需要建造什么小人,而CreatPerson调用建造过程就行
PersonDirector::PersonDirector(PersonBuilder * person_builder):m_person_builder(person_builder){
}void PersonDirector::CreatPerson()
{m_person_builder->BuildHead();m_person_builder->BuildBody();m_person_builder->BuildLeg();
}
以上完成了所有类的实现,各类之间关系如下
main函数就很好写出来了,只需要向指挥者传入想创建的小人就行
int main()
{Pen *p = new Pen;Graphics *g = new Graphics;PersonBuilder *build1 = new BuildThinPerson(p,g);PersonDirector *dir = new PersonDirector(build1);dir->CreatPerson();PersonBuilder *build2 = new BuildFatPerson(p, g);dir = new PersonDirector(build2);dir->CreatPerson();delete p;delete g;delete build1;delete dir;return 0;
}
完成题设要求
所有代码如下:
build.h
#ifndef BUILD_H
#define BUILD_H
class Pen {
public:Pen();
};class Graphics {
public:Graphics();
};class PersonBuilder {
protected:Pen *m_pen;Graphics *m_graphics;
public:PersonBuilder(Pen *pen,Graphics *graphics);virtual ~PersonBuilder() {};virtual void BuildHead() {};virtual void BuildBody() {};virtual void BuildLeg() {};
};class BuildThinPerson:public PersonBuilder{
public:BuildThinPerson(Pen *pen, Graphics *graphics);virtual void BuildHead();virtual void BuildBody();virtual void BuildLeg();
};class BuildFatPerson:public PersonBuilder {
public:BuildFatPerson(Pen *pen, Graphics *graphics);virtual void BuildHead();virtual void BuildBody();virtual void BuildLeg();
};class PersonDirector {
private:PersonBuilder* m_person_builder;
public:PersonDirector(PersonBuilder* person_builder);void CreatPerson();
};#endif
build.cpp
#include"build.h"
#include<iostream>
using namespace std;
Pen::Pen(){cout << "use yellow pen" << endl;
}Graphics::Graphics(){cout << "start to write" << endl;
}PersonBuilder::PersonBuilder(Pen* pen, Graphics* graphics) : m_pen(pen), m_graphics(graphics) {}BuildThinPerson::BuildThinPerson(Pen* pen, Graphics* graphics): PersonBuilder(pen,graphics){
}void BuildThinPerson::BuildHead() {cout << "build Head of thin person" << endl;
}void BuildThinPerson::BuildBody() {cout << "build Body of thin person" << endl;
}void BuildThinPerson::BuildLeg() {cout << "build Leg of thin person" << endl;
}BuildFatPerson::BuildFatPerson(Pen * pen, Graphics * graphics) :PersonBuilder(pen, graphics){
}void BuildFatPerson::BuildHead() {cout << "build Head of fat person" << endl;
}void BuildFatPerson::BuildBody() {cout << "build Body of fat person" << endl;
}void BuildFatPerson::BuildLeg() {cout << "build Leg of fat person" << endl;
}PersonDirector::PersonDirector(PersonBuilder * person_builder):m_person_builder(person_builder){
}void PersonDirector::CreatPerson()
{m_person_builder->BuildHead();m_person_builder->BuildBody();m_person_builder->BuildLeg();
}
main.cpp
#include"build.h"int main()
{Pen *p = new Pen;Graphics *g = new Graphics;PersonBuilder *build1 = new BuildThinPerson(p,g);PersonDirector *dir = new PersonDirector(build1);dir->CreatPerson();PersonBuilder *build2 = new BuildFatPerson(p, g);dir = new PersonDirector(build2);dir->CreatPerson();delete p;delete g;delete build1;delete dir;return 0;
}
4.建造者模式
建造者模式:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。用户只需要指定需要建造的类型就可以的得到他们,而具体的构建过程和细节不需要知道。
如下图所示,Builder是创建小人的抽象类,具体建造者继承他进行构造和装配。
建造者模式主要用于创建一些复杂对象,这些对象内部构造间的顺序通常是稳定的,但对象内部的构造通常面临复杂变化。
这篇关于大话设计模式8—建造者模式(建造小人)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!