设计模式(一)Singleton,Command
来源:互联网 发布:系统管理员数据流程图 编辑:程序博客网 时间:2024/06/05 03:40
//: 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;
} ///:~
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();
} ///:~
- 设计模式(一)Singleton,Command
- 设计模式(一):Singleton模式
- 设计模式(一):Singleton模式
- 设计模式(一)Singleton 模式
- java设计模式(一):Singleton(单例模式)
- 设计模式(一):单例模式(Singleton Pattern)
- 26种设计模式--(一)单例模式 Singleton
- 设计模式(一)单例模式Singleton
- java设计模式一(单例模式singleton)
- 设计模式一日一练:单件模式(Singleton)
- 设计模式(一)单例模式 Singleton pattern
- java设计模式----单例模式Singleton(一)
- 设计模式实践(一)单例模式(Singleton)
- 设计模式 一 单例模式Singleton
- 设计模式一之Singleton模式
- Head First 设计模式 Design Pattern 5-6 Singleton, Command
- 设计模式学习笔记(一)——Singleton
- (C#设计模式一)Singleton(单态)
- c#操作DBF数据库文件
- 小结2010,展望2011
- VC 文件对话框 目录选择
- 嵌入式Linux系统工程师学习大纲
- TFS文件系统策略分析
- 设计模式(一)Singleton,Command
- 设计模式
- JS的全选与删除
- VC6.0 VS2008 openGL 环境配置
- WINCE BSB中dirs, sources,sources.cmn文件
- C++基础之:引用与指针的区别
- VMware改变硬盘空间大小的方法
- C语言关键字----Const
- 用C实现程序的多态性