`

设计模式(一)Singleton,Command

阅读更多
//: C10:SingletonPattern.cpp
#include <iostream>
using namespace std;
 
class Singleton {
  static Singleton s;
  int i;
  Singleton(int x) : i(x) { }
  Singleton& operator=(Singleton&);  // Disallowed
  Singleton(const Singleton&);       // Disallowed
public:
  static Singleton& instance() { return s; }
  int getValue() { return i; }
  void setValue(int x) { i = x; }
};
 
Singleton Singleton::s(47);
 
int main() {
  Singleton& s = Singleton::instance();
  cout << s.getValue() << endl;
  Singleton& s2 = Singleton::instance();
  s2.setValue(9);
  cout << s.getValue() << endl;
} ///:~
<style type="text/css"> .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }</style>

Command:

可以说,命令模式是携带行为信息的信使。

//: C10:CommandPattern.cpp
#include <iostream>
#include <vector>
using namespace std;


  
class Command {
public:
    virtual void execute() = 0;
};


  
class Hello : public Command {
public:
    void execute() { cout << "Hello "; }
};


  
class World : public Command {
public:
    void execute() { cout << "World! "; }
};


  
class IAm : public Command {
public:
    void execute() { cout << "I'm the command pattern!"; }
};


  
// An object that holds commands:
class Macro {
    vector<Command*> commands;
public:
    void add(Command* c) { commands.push_back(c); }
    void run() {
        vector<Command*>::iterator it = commands.begin();
        while(it != commands.end())
            (*it++)->execute();
    }
};


  
int main() {
    Macro macro;
    macro.add(new Hello);
    macro.add(new World);
    macro.add(new IAm);
    macro.run();
} ///:~
<style type="text/css"> .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }</style> <style type="text/css"> .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }</style>
分享到:
评论

相关推荐

    JAVA设计模式chm文档

    设计模式之Singleton(单态) 结构模式: 设计模式之Facade(外观) 设计模式之Proxy(代理) 设计模式之Adapter(适配器) 设计模式之Composite(组合) 设计模式之Decorator(油漆工) 设计模式之Bridge 设计模式之Flyweight(享...

    设计模式精解-GoF 23种设计模式解析

    设计模式精解-GoF 23 种设计模式解析附 C++实现源码 目 录 引 言 0.1 设计模式解析(总序) 0.2 设计模式解析后记 0.3 与作者联系 1 创建型模式 1.1 Factory模式 1.2 AbstactFactory模式 1.3 Singleton...

    设计模式文档 chm

    设计模式之Singleton(单态) 结构模式: 设计模式之Facade(外观) 设计模式之Proxy(代理) 设计模式之Adapter(适配器) 设计模式之Composite(组合) 设计模式之Decorator(油漆工) 设计模式之Bridge 设计模式之Flyweight(享...

    研磨设计模式(完整带书签).part2.pdf

    如果你想要深入透彻地理解和掌握设计模式,并期望能真正把设计模式应用到项目中去,那么这是你不可错过的一本好书。 《研磨设计模式》难度为初级到中级,适合与所有开发人员、设计人员或者即将成为开发人员的朋友。...

    C#设计模式.PDF

    C#设计模式(7)-Singleton Pattern 50 一、 单例(Singleton)模式 50 二、 Singleton模式的结构: 51 三、 程序举例: 51 四、 在什么情形下使用单例模式: 52 五、 Singleton模式在实际系统中的实现 53 六、 C#...

    C#面向对象设计模式纵横谈(视频与源码)

    C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式) C#面向对象设计模式纵横谈(3):Abstract Factory 抽象工厂模式(创建型模式) C#面向对象设计模式纵横谈(4):Builder 生成器模式(创建型模式) C#面向...

    36种最新设计模式整理

    36种最新设计模式整理 Design Pattern: Simple Factory 模式 Design Pattern: Abstract Factory 模式 Design Pattern: Builder 模式 Design Pattern: Factory Method 模式 Design Pattern: Prototype 模式 ...

    Head First设计模式.pdf (高清)

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...

    Head First设计模式

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、AbstractFactory、FactoryMethod、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较特别...

    33种JAVA设计模式DEMO

    这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式,而不是使用 new 运算符直接实例化对象。这使得程序在判断针对某个给定实例需要创建哪些对象时更加灵活。 工厂模式(Factory Pattern) 抽象工厂模式...

    HeadFirst设计模式(中文版)

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm” 供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) ...

    设计模式可复用面向对象软件的基础.zip

    本书分类描述了一组设计良好,表达清楚的软件设计模式,这些模式在实用环境下有特别有用。 -------- 目 录 序言 前言 读者指南 第1章 引言 1 1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计...

    大优惠Head First 设计模式

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...

    C#设计模式-吕震宇

    设计模式(18)-Command Pattern 设计模式(17)-Chain of Responsibility Pattern 设计模式(16)-Bridge Pattern 设计模式(15)-Facade Pattern 设计模式(14)-Flyweight Pattern C#设计模式(13)-Proxy ...

    论文研究-设计模式在标图系统中的综合应用.pdf

    按照设计模式中“高内聚、低耦合”的原则,设计出一种标图系统的整体结构,并以Prototype, Template Method, Observer, Command, Composite和Singleton六种具体模式为例,详细阐述了它们在系统功能实现过程中的应用...

    head first 设计模式

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...

    Head.First设计模式_PDF

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、Template Method、iterator、Composite、State、proxy。最后三章比较...

    Head First 设计模式(中文版)

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...

    C#设计模式大全

    C#设计模式(7)-Singleton Pattern 一、 单例(Singleton)模式 二、 Singleton模式的结构: 三、 程序举例: 四、 在什么情形下使用单例模式: 五、 Singleton模式在实际系统中的实现 六、 C#中的Singleton...

Global site tag (gtag.js) - Google Analytics