c++设计模式(4)------建造者模式

建造者模式简介

1)适合的场景:对于对象的创建很复杂,而且对象的创建过程可以任意组合的情况。
2)举例:建造房子,要建造墙壁、窗户、门;而且假设可以以任何顺序来建造,那么这个的情景就适合建造者模式
3)类图的表示:


#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
class Home
{
public:
    Home()
    {}
    Home(string wall , string door , string window):m_wall(wall),m_door(door),m_window(window)
    {}

public:
    string getDoor()
    {
        return m_door;
    }
    string getWindow()
    {
        return m_window;
    }
    string getWall()
    {
        return m_wall;
    }
    void setDoor(string str)
    {
        m_door = str;
    }
    void setWindow(string str)
    {
        m_window = str;
    }
    void setWall(string str)
    {
        m_wall = str;
    }
private:
    string m_wall;
    string m_door;
    string m_window;

};
//建筑师是控制产品的创建,而且产品的创建比较复杂
class Builder
{
public:

    virtual void createWall() = 0;
    virtual void createDoor() = 0;
    virtual void createWindow() = 0;
};
class Builder_Falt : public Builder
{
public:
    Builder_Falt(Home * home):m_home(home)
    {

    }
    virtual void createWall()
    {
        cout<<"this is creating wall  平方 "<<endl;
        cout<<"wall name "<<m_home->getWall()<<endl;
    }
    virtual void createDoor() 
    {
        cout<<"this is creating door 平方"<<endl;
        cout<<"door name "<<m_home->getDoor()<<endl;
    }
    virtual void createWindow()
    {
        cout<<"this is creating window 平方"<<endl;
        cout<<"window name "<<m_home->getWindow()<<endl;
    }
private:
    Home * m_home;
};
class Builder_gongyu : public Builder
{
public:
    Builder_gongyu(Home * home):m_home(home)
    {

    }
    virtual void createWall()
    {
        cout<<"this is creating wall gongyu "<<endl;
        cout<<"wall name "<<m_home->getWall()<<endl;
    }
    virtual void createDoor() 
    {
        cout<<"this is creating door  gongyu"<<endl;
        cout<<"door name "<<m_home->getDoor()<<endl;
    }
    virtual void createWindow()
    {
        cout<<"this is creating window gongyu "<<endl;
        cout<<"window name "<<m_home->getWindow()<<endl;
    }
private:
    Home * m_home;
};
//设计师控制产品的建造逻辑
class Designer
{
public:
    Designer(Builder* builde)
    {
        m_builder = builde;
    }
    void createHome()
    {
        m_builder->createDoor();
        m_builder->createWindow();
        m_builder->createWall();
    }
private:
    Builder* m_builder;
};

int main()
{

    Home * home = new Home;
    home->setDoor("门");
    home->setWindow("窗户");
    home->setWall("墙壁");
    Builder* bt = new Builder_gongyu(home);
    Designer * ds = new Designer(bt);
    ds->createHome();
    cout<<"hello world"<<endl;
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值