Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119285
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 242
  • 用 户 组: 普通用户
  • 注册时间: 2014-07-17 13:36
文章分类

全部博文(29)

文章存档

2015年(29)

我的朋友

分类: C/C++

2015-06-08 10:19:08


本例子为单例模式与命令模式的结合。

例子中,涉及三个组:软件组、硬件组、测试组。

每个组的获得使用单例设计模式。

每个组的命令执行,使用命令模式。

以下为实际代码:

 

#include

using namespace std;

 

 

class Group

{

public:

    virtual void AddTask() = 0;

    virtual void DeleteTask() = 0;

    virtual void ChangeTask() = 0;

};

 

class SoftwareGroup : public Group

{

public:

    virtual void AddTask(){cout << "Add Software Task" << endl;}

    virtual void DeleteTask(){cout << "Delete Software Task" << endl;}

    virtual void ChangeTask(){cout << "Change Software Task" << endl;}

    static SoftwareGroup& GetGroup(){return m_sg;}

private:

    SoftwareGroup(){}

    SoftwareGroup(const SoftwareGroup& software){}

    static SoftwareGroup m_sg;

};

SoftwareGroup SoftwareGroup::m_sg;

 

class HardwareGroup

{

public:

    virtual void AddTask(){cout << "Add Hareware Task" << endl;}

    virtual void DeleteTask(){cout << "Delete Hardware Task" << endl;}

    virtual void ChangeTask(){cout << "Change Hardware Task" << endl;}

    static HardwareGroup& GetGroup(){return m_hg;}

private:

    HardwareGroup(){}

    HardwareGroup(const HardwareGroup&){}

    static HardwareGroup m_hg;

};

HardwareGroup HardwareGroup::m_hg;

 

class TestGroup

{

public:

    virtual void AddTask(){cout << "Add Test Task" << endl;}

    virtual void DeleteTask(){cout << "Delete Test Task" << endl;}

    virtual void ChangeTask(){cout << "Change Test Task" << endl;}

    static TestGroup& GetGroup(){return m_tg;}

private:

    TestGroup(){}

    TestGroup(const TestGroup&){}

    static TestGroup m_tg;

};

TestGroup TestGroup::m_tg;

 

class Command

{

public:

    virtual void DoSomething() = 0;

};

//software commands

class SoftCommand : public Command

{

public:

    SoftCommand():m_sg(SoftwareGroup::GetGroup()){}

protected:

    SoftwareGroup& m_sg;

};

class SoftAddCmd : public SoftCommand

{

public:

    void DoSomething(){m_sg.AddTask();}

};

class SoftDeleteCmd : public SoftCommand

{

public:

    void DoSomething(){m_sg.DeleteTask();}

};

class SoftChangeCmd : public SoftCommand

{

public:

    void DoSomething(){m_sg.ChangeTask();}

};

//hardware commands

class HardCommand : public Command

{

public:

    HardCommand():m_hg(HardwareGroup::GetGroup()){}

protected:

    HardwareGroup& m_hg;

};

class HardAddCmd : public HardCommand

{

public:

    void DoSomething(){m_hg.AddTask();}

};

class HardDeleteCmd : public HardCommand

{

public:

    void DoSomething(){m_hg.DeleteTask();}

};

class HardChangeCmd : public HardCommand

{

public:

    void DoSomething(){m_hg.ChangeTask();}

};

//test commands

class TestCommand : public Command

{

public:

    TestCommand():m_tg(TestGroup::GetGroup()){}

    void DoSomething(){}

protected:

    TestGroup& m_tg;

};

class TestAddCmd : public TestCommand

{

public:

    void DoSomething(){m_tg.AddTask();}

};

class TestDeleteCmd : public TestCommand

{

public:

    void DoSomething(){m_tg.DeleteTask();}

};

class TestChangeCmd : public TestCommand

{

public:

    void DoSomething(){m_tg.ChangeTask();}

};

 

class Invoker

{

public:

    void Execute(Command* cmd){cmd->DoSomething();}

};

 

 

int main()

{

    Invoker invoker;

    Command* p_cmd = 0;

   

    TestAddCmd cmd1;

    p_cmd = &cmd1; 

    invoker.Execute( p_cmd );

   

    SoftDeleteCmd cmd2;

    p_cmd = &cmd2;

    invoker.Execute( p_cmd );

   

    SoftChangeCmd cmd3;

    p_cmd = &cmd3;

    invoker.Execute( p_cmd );

    return 0;

}

 

输出结果:

Add Test Task

Delete Software Task

Change Software Task

 

 


阅读(1260) | 评论(0) | 转发(0) |
0

上一篇:自己实现list与迭代器

下一篇:HTML4格式

给主人留下些什么吧!~~