C++工程,C++设计模式-建造者模式

c++设计模式
建造者模式:将复杂对象的构建和其表示分离,使得相同的构建过程可以产生不同的表示。

以下情形可以考虑使用建造者模式:

  • 对象的创建复杂,但是其各个部分的子对象创建算法一定。
  • 需求变化大,构造复杂对象的子对象经常变化,但将其组合在一起的算法相对稳定。

建造者模式的优点:

  • 将对象的创建和表示分离,客户端不需要了解具体的构建细节。
  • 增加新的产品对象时,只需要增加其具体的建造类即可,不需要修改原来的代码,扩展方便。

产品之间差异性大,内部变化较大、较复杂时不建议使用建造者模式。

1,运行效果

在这里插入图片描述

2,代码示例

#include <iostream>
#include <typeinfo>
#include <vector>

class Leaf;

//define a abstract class of trees
class Tree {
    public:
        virtual int height(void) = 0;
        virtual std::string name(void) = 0;
        virtual Leaf *leaf(void) = 0;
        virtual ~Tree() {};
    protected:
        int m_height;
        std::string m_name;
        Leaf *m_leaf;
};

//define a abstract class of leaf
class Leaf {
    public:
        virtual std::string leafType() = 0;
        virtual ~Leaf() {};
};

//specific leaf class
class BroadLeaf : public Leaf {
    public:
        BroadLeaf() {};
        std::string leafType() override {return "broad_leaf";};
};
//specific leaf class
class NeedleLeaf : public Leaf {
    public:
        NeedleLeaf() {};
        std::string leafType() override {return "needle_leaf";};
};

//specific tree class
class PineTree : public Tree {
    public:
        PineTree() {m_height=100; m_name=typeid(*this).name(); m_leaf=(Leaf*)new NeedleLeaf();};
        ~PineTree() {delete m_leaf; m_leaf=nullptr;};
        int height(void) override {return m_height;};
        std::string name(void) override {return m_name;};
        Leaf *leaf(void) override {return m_leaf;}
};
//specific tree class
class CypressTree : public Tree {
    public:
        CypressTree() {m_height=80; m_name=typeid(*this).name(); m_leaf=(Leaf*)new NeedleLeaf();};
        ~CypressTree() {delete m_leaf; m_leaf=nullptr;};
        int height(void) override {return m_height;};
        std::string name(void) override {return m_name;};
        Leaf *leaf(void) override {return m_leaf;}
};
//specific tree class
class WillowTree : public Tree {
    public:
        WillowTree() {m_height=50; m_name=typeid(*this).name(); m_leaf=(Leaf*)new BroadLeaf();};
        ~WillowTree() {delete m_leaf; m_leaf=nullptr;};
        int height(void) override {return m_height;};
        std::string name(void) override {return m_name;};
        Leaf *leaf(void) override {return m_leaf;}
};

class Manage {
    public:
        Manage() {};
        ~Manage() {for(auto iter:m_trees){delete iter;}};
        void addTree(Tree *tree) {m_trees.push_back(tree);};
        void print(void) {
            for(auto iter : m_trees) {
                std::cout << "name:"<< iter->name()
                        << "  height:" << iter->height()
                        << "  leaf:" << iter->leaf()->leafType() << std::endl;
            }
        }
    private:
        std::vector<Tree*> m_trees;
};
int main() {
    Manage manage;

    manage.addTree((Tree*)new PineTree());
    manage.addTree((Tree*)new CypressTree());
    manage.addTree((Tree*)new WillowTree());

    manage.print();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值