设计模式《十》——组合模式

简介

       组合模式又叫部分-整体模式,用于把一组相似的对象当作一个单一的对象,依据树状结构来组合对象。使得用户对单个对象和组合对象的使用具有一致性。例如,公司职员组织关系。

 

角色与职责

 

Component:组合类中的抽象接口,在适当的情况下,实现所有类共有接口的默认行为

Composite:有枝节点行为,用于存储子部件,在Component接口中实现与子部件有关的操作,比如Add和Remove操作等。

Leaf:在组合中表示叶节点对象,叶节点没有子节点,展示自己。

 

实现

案例:文件目录,定义一个文件和目录统一接口类Component,Leaf代表文件类(没有子节点,直接显示自己的文件名),Composite代表子目录类。

#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
// 目录和文件都使用相同的接口。
class IFile {
public:
    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) {
        m_name = name;
    }
    virtual void display() {
        cout << m_name << endl;
    }
    virtual int add(IFile* ifile) {
        return -1;
    }
    virtual int remove(IFile* ifile) {
        return -1;
    }
    virtual list<IFile*>* getChild() {
        return NULL;
    }
private:
    string m_name;
    list<IFile*> m_list;
};
// 目录对象
class Dir : public IFile{
public:
    Dir(string name) {
        m_name = name;
        m_list = new list<IFile*>;
        m_list->clear();
    }
    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;
};
struct play{
    void operator()(IFile* it) {
        it->display();
    }
};
// 分层递归显示所有目录和文件
void show(IFile* root, int level) {
    if (NULL == root)   return;
    list<IFile*>* flist = NULL;
    for (int i = 0; i < level; i++) {
        printf("\t");
    }
    root->display();
    flist = root->getChild();
    if (flist != NULL) {
        for (list<IFile*>::iterator it = flist->begin(); it != flist->end(); it++) {
            if ((*it)->getChild() == NULL) {
                for (int i = 0; i < level; i++) {
                    printf("\t");
                }
                (*it)->display();
            }
            else {
                show((*it), level + 1);
            }
        }
    }
}
int main(int argc, char* argv[]) {
    Dir* root = new Dir("C");
    Dir* dir1 = new Dir("aaa");
    File* file123 = new File("111.txt");
    root->add(dir1);        // C盘下增加目录abc
    root->add(file123);     // C盘下增加文件123.txt
    Dir* dir2 = new Dir("bbb");
    File* file222 = new File("222.txt");
    dir1->add(dir2);
    dir1->add(file222);
    // 显示单层文件和目录
    //root->display();
    //list<IFile*>* mylist = dir1->getChild();
    //for_each(mylist->begin(), mylist->end(), play());
    // 显示所有目录和文件
    show(root, 0);
    cin.get();
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值