类只能实例化一次
2009-09-24 08:46
127 查看
昨天听到同学讨论的一个问题,问题是:类如何实例化一次。
上网查了下,查到了23种设计模式的问题。
创建型:
1. 单件模式(Singleton Pattern)
2. 抽象工厂(Abstract Factory)
3. 建造者模式(Builder)
4. 工厂方法模式(Factory Method)
5. 原型模式(Prototype)
结构型:
6. 适配器模式(Adapter Pattern)
7. 桥接模式(Bridge Pattern)
8. 装饰模式(Decorator Pattern)
9. 组合模式(Composite Pattern)
10. 外观模式(Facade Pattern)
11. 享元模式(Flyweight Pattern)
12. 代理模式(Proxy Pattern)
13. 模板方法(Template Method)
14. 命令模式(Command Pattern)
15. 迭代器模式(Iterator Pattern)
行为型:
16. 观察者模式(Observer Pattern)
17. 解释器模式(Interpreter Pattern)
18. 中介者模式(Mediator Pattern)
19. 职责链模式(Chain of Responsibility Pattern)
20. 备忘录模式(Memento Pattern)
21. 策略模式(Strategy Pattern)
22. 访问者模式(Visitor Pattern)
23. 状态模式(State Pattern)
而类的只能实例化一次,是其中的一种,单件模式的一种方法。
我一开始只认为,实例化一次,是只能定义一次而已。而事实证明是错的。正确的说,是内存中,只能有一个这样的实例。结合网上的例子,加了点代码,证明上面的想法。
#include<iostream>
using namespace std;
class Singleton
{
public:
static Singleton* Instance();
int b;
protected:
Singleton(){};
private:
static Singleton* _instance;
};
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance () {
if (_instance == 0) {
_instance = new Singleton;
}
return _instance;
}
int main()
{
Singleton *a=Singleton::Instance();
a->b=10;
Singleton *b=Singleton::Instance();
cout<<b->b<<endl;
}
上面输出的b->b为10。。
上网查了下,查到了23种设计模式的问题。
创建型:
1. 单件模式(Singleton Pattern)
2. 抽象工厂(Abstract Factory)
3. 建造者模式(Builder)
4. 工厂方法模式(Factory Method)
5. 原型模式(Prototype)
结构型:
6. 适配器模式(Adapter Pattern)
7. 桥接模式(Bridge Pattern)
8. 装饰模式(Decorator Pattern)
9. 组合模式(Composite Pattern)
10. 外观模式(Facade Pattern)
11. 享元模式(Flyweight Pattern)
12. 代理模式(Proxy Pattern)
13. 模板方法(Template Method)
14. 命令模式(Command Pattern)
15. 迭代器模式(Iterator Pattern)
行为型:
16. 观察者模式(Observer Pattern)
17. 解释器模式(Interpreter Pattern)
18. 中介者模式(Mediator Pattern)
19. 职责链模式(Chain of Responsibility Pattern)
20. 备忘录模式(Memento Pattern)
21. 策略模式(Strategy Pattern)
22. 访问者模式(Visitor Pattern)
23. 状态模式(State Pattern)
而类的只能实例化一次,是其中的一种,单件模式的一种方法。
我一开始只认为,实例化一次,是只能定义一次而已。而事实证明是错的。正确的说,是内存中,只能有一个这样的实例。结合网上的例子,加了点代码,证明上面的想法。
#include<iostream>
using namespace std;
class Singleton
{
public:
static Singleton* Instance();
int b;
protected:
Singleton(){};
private:
static Singleton* _instance;
};
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance () {
if (_instance == 0) {
_instance = new Singleton;
}
return _instance;
}
int main()
{
Singleton *a=Singleton::Instance();
a->b=10;
Singleton *b=Singleton::Instance();
cout<<b->b<<endl;
}
上面输出的b->b为10。。
相关文章推荐
- 只能实例化一次的类
- 只能实例化一次的类 (Singleton)
- 只能实例化一次的类 (Singleton)
- 实现一个只能实例化一次的类 即 单例模式(Singleton)
- 用C#实现一个类,要求该类只能被实例化一次
- 只能被实例化一次的类
- 实现一个类只能被实例化一次
- a标签只能点击一次/a标签点击一次后失效
- 只能登录一次
- 一个线程只能被启动一次
- signal 函数只能接受一次信号? The naughty signal function
- jquery checkbox全选,全不选,反选方法,jquery checkbox全选只能操作一次
- Hadoop提供的reduce函数中Iterable 接口只能遍历一次的问题
- PYQt4运行界面只能显示一次之后出现下面的错误:It seems the kernel died unexpectedly. Use 'Restart kernel' to continue usi
- 全选checkbox只能执行一次的问题
- JavaScript-RegExp对象只能使用一次问题解决方法
- C#应用程序只能打开一次,应用程序只能打开一个,应用程序不能重复打开
- 关于 创建 只能 实例化两个 对象的单体类
- AutoMapper: Mapper.Initialize() 只能调用一次,Why?
- C#实现微信公众号群发消息(解决一天只能发一次的限制)