天天看点

C++工程,C++设计模式-建造者模式1,运行效果2,代码示例

文章目录

  • 1,运行效果
  • 2,代码示例

c++设计模式

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

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

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

建造者模式的优点:

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

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

1,运行效果

C++工程,C++设计模式-建造者模式1,运行效果2,代码示例

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;
}
           

继续阅读