设计模式----组合模式

组合模式

合成模式将对象组织到树结构中,可以用来描述整体与部分的关系。合成模式就是一个处理对象的树结构的模式。合成模式把部分与整体的关系用树结构表示出来。合成模式使得客户端把一个个单独的成分对象和由他们复合而成的合成对象同等看待。
Mary今天过生日。“我过生日,你要送我一件礼物。”“嗯,好吧,去商店,你自己挑。”“这件T恤挺漂亮,买,这 条裙子好看,买,这个包也不错,买。”“喂,买了三件了呀,我只答应送一件礼物的哦。”“什么呀,T恤加裙子加包包,正好配成一套呀,小姐,麻烦你包起来。

#include <iostream>
#include <List>
using namespace std;

class IFile{
    public:
        virtual ~IFile(){}
        virtual void display()=0;
        virtual int add(IFile *iFile)=0;
        virtual int remove(IFile *iFile)=0;
        virtual list<IFile*> *getChild()=0;
};

class File : public IFile{
    public:
        File(string name){
            this->name = name;
        }
        virtual void display(){
            std::cout << name << std::endl;
        }
        virtual int add(IFile* iFile) {
            return -1;  
        }
        virtual int remove(IFile* iFile) {
            return -1;  
        }
        virtual list<IFile*>* getChild() {
            return NULL;    
        }

    private:
        string name;
};

class Folder : public IFile{
    public:
        Folder(string name) {
            m_name = name;
            m_list = new list<IFile*>;
            m_list->clear();
        }
        virtual ~Folder() {
            if (m_list != NULL) {
                delete m_list;
                m_list = NULL;
            }
        }
        virtual void display() {
            cout << m_name << ":" << endl;  
        } 
        virtual int add(IFile* iFile) {
            m_list->push_back(iFile);
            return 0;
        }
        virtual int remove(IFile* iFile) {
            m_list->remove(iFile);
            return 0;
        }
        virtual list<IFile*>* getChild() {
            return m_list;
        }
    private:
        string m_name;
        list<IFile*>* m_list;
};


void showTree(IFile *root, int level){
    if(root==NULL){
        return;
    }
    for(int i = 0;i<level;i++){
        std::cout << "\t" << std::endl;
    }
    root->display();
    list<IFile *> *myList = root->getChild();
    if(myList==NULL){
        return;
    } else{
        for(list<IFile *>::iterator it = myList->begin(); it != myList->end(); ++it) {
            if ((*it)->getChild() == NULL) {
                for(int i = 0; i < level + 1; ++i) {
                    cout << "\t";
                }
                (*it)->display();   
            } else {
                showTree(*it, level + 1);
            }
        }
    }
}

int main()
{
    Folder* root = new Folder("C");
    File* file1 = new File("file1.txt");
    root->add(file1);

    Folder* folder1 = new Folder("folder1");
    File* file2 = new File("file2.txt");
    folder1->add(file2);
    root->add(folder1);

    showTree(root, 0);
    
    delete file1;
    delete folder1;
    delete root;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值