设计模式(2)

观察者模式:
Observer模式是行为模式之一,它的作用是当一个对象的状态发生变化时,能够自动通知其他关联对象,自动刷新对象状态。
Observer模式提供给关联对象一种同步通信的手段,使某个对象与依赖它的其他对象之间保持状态同

include

include

include

using namespace std;

class Assist;
// 抽象观察者
class Observer
{
public:
virtual void subscribe(Assist &a) = 0; // 注册
virtual void disSubscribe(Assist &a) = 0; // 注册
virtual void update(string info) = 0; // 更新自己
};

// 老板助手
class Assist
{
public:
void add(Observer *o)
{
m_list.push_back(o);
}

void remove(Observer *o)
{
    m_list.remove(o);
}
void notify(string info)
{
    for (list<Observer*>::iterator it = m_list.begin(); it != m_list.end(); it++)
    {
        (*it)->update(info);    // 挨个通知
    }
}

private:
list

#include <iostream>
#include <list>
#include <string>
#include <algorithm>
using namespace std;

// 定义同一的操作
class IFile
{
public:
    virtual void showName() = 0;
    virtual string getName() = 0;
    virtual void add(IFile *file) = 0;
    virtual void remove(IFile *file)= 0;
    virtual list<IFile *>* getChildList() = 0;


    // 对象复制一份自己的数据,然后将新对象指针返回给别人用
    virtual IFile * clone() = 0;
protected:
    string name;
};

// 普通文件
class File :public IFile
{
public:
    File (string name)
    {
        this->name = name;
    }
    string getName()
    {
        return name;
    }
    void showName()
    {
        cout << name << endl;
    }
    void add(IFile *file)
    {
        return;
    }
    void remove(IFile *file)
    {
        return;
    }
    list<IFile *>* getChildList()
    {
        return NULL;
    }
    IFile * clone()
    {
        // 1、
        // File *newFile = new File(*this);   // 拷贝构造

        // 2、
        File *newFile = new File("");   // 创建一个对象
        // *newFile = *this;  // 调用赋值运算符重载


        // 3、手动数据赋值
        newFile->name = this->name;
        return newFile;
    }

};

// 目录
class Dir:public IFile
{
public:
    Dir (string name)
    {
        this->name = name;
        m_list = new list<IFile *>;
    }
    string getName()
    {
        return name;
    }
    ~Dir()
    {
        if (m_list != NULL)
        {
            list<IFile *>::iterator it = m_list->begin();
            while (it != m_list->end())
            {
                delete (*it);   // 先释放文件指针内存
                it = m_list->erase(it);
            }

            delete m_list;
        }
    }
    void showName()
    {
        cout << name << endl;
    }
    void add(IFile *file)
    {
        m_list->push_back(file);
        return;
    }
    void remove(IFile *file)
    {
        m_list->remove(file);
        delete file;
        return;
    }
    list<IFile *>* getChildList()
    {
        return m_list;
    }
    // 复制一份自己的数据,把新创建的对象返回出去
    IFile * clone()
    {
        // 1、
        // Dir *newDir = new Dir(*this);   // 拷贝构造

        // 2、
        Dir *newDir = new Dir("");   // 创建一个对象
        // *newFile = *this;  // 调用赋值运算符重载


        // 3、手动数据赋值
        newDir->name = this->name;
        // newDir->m_list = this->m_list;

        for (list<IFile*>::iterator it = m_list->begin(); it != m_list->end(); it++)
        {
            IFile* pf = (*it)->clone();
            newDir->add(pf);
        }

        return newDir;
    }
private:
    list<IFile *> *m_list;
};

void init(IFile *root)
{
    IFile *dir1 = new Dir("学习资料");
    IFile *dir2 = new Dir("游戏");
    IFile *dir3 = new Dir("电影");
    IFile *dir4 = new Dir("仙剑");
    IFile *dir5 = new Dir("星战");

    IFile *file1 = new File("面试大全.pdf");
    IFile *file2 = new File("C++经典面试题.pdf");
    IFile *file3 = new File("LOL.exe");
    IFile *file4 = new File("荒野行动.exe");
    IFile *file5 = new File("仙剑1.exe");
    IFile *file6 = new File("仙剑2.exe");
    IFile *file7 = new File("星战4.mp4");
    IFile *file8 = new File("星战8.mp4");
    IFile *file9 = new File("羞羞的铁拳.mp4");

    root->add(dir1);
    root->add(dir2);
    root->add(dir3);

    dir1->add(file1);
    dir1->add(file2);

    dir2->add(file3);
    dir2->add(file4);
    dir2->add(dir4);

    dir3->add(dir5);
    dir3->add(file9);

    dir4->add(file5);
    dir4->add(file6);

    dir5->add(file7);
    dir5->add(file8);
}


void showNode(IFile *node, int gap)
{
    if (node == NULL)
        return;

    for (int i = 0; i < gap; i++)
        printf ("    ");

    // 1、显示当前结点
    node->showName();

    // 2、显示孩子结点

    // 获取孩子结点链表
    list<IFile*>* pd = node->getChildList();
    if (pd == NULL)  // 文件没有孩子结点链表
        return;

    // 从孩子结点链表中一个一个取出孩子结点
    for (list<IFile*>::iterator it = pd->begin(); it != pd->end(); it++)
    {
        // 显示孩子结点内容
        showNode(*it, gap+1);
    }
}

class mySearch
{
public:
    mySearch(string name)
    {
        this->name = name;
    }
public:
    bool operator()(IFile *node)
    {
        return node->getName() == name;
    }

private:
    string name;
};

void showDir(IFile *root, string name)
{
    list<IFile*>* pd = root->getChildList();
    list<IFile*>::iterator it = find_if(pd->begin(), pd->end(), mySearch(name));
    if (it == pd->end())
        return;

    showNode(*it, 0);
}

int main()
{
    // 文件系统
    IFile *root = new Dir("D:");
    init(root);

    list<IFile*>* pd = root->getChildList();
    list<IFile*>::iterator it = find_if(pd->begin(), pd->end(), mySearch("游戏"));
    // showDir(*it, "仙剑");

    // Dir(const Dir &dir)   Dir(IFile *p)
    IFile *pf = root->clone();

    showNode(pf, 0);

    //list<IFile*>::iterator it = pd->begin();
    //for (; it != pd->end(); it++)
    //{
    //  (*it)->showName();
    //}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值