Item 29. Virtual Constructors and Prototype&Item 30. Factory Method
来源:互联网 发布:eraser扣图软件 编辑:程序博客网 时间:2024/06/13 11:15
Item 29. Virtual Constructors and Prototype
Virtual Constructors?
C++没有Virtual Constructors这个概念。但有的情况下又需要这个功能,即在多态的时候能够创建出一个跟指针的动态类型相同的一个对象。既然是多态就要考虑虚函数,在虚函数中调用构造函数不就行了么?是的,这就是设计模式中的Prototype的解决方案:一个clone()的虚成员函数。
class Meal {
public:
virtual ~Meal();
virtual void eat() = 0;
virtual Meal *clone() const = 0;
//...
};
class Spaghetti : public Meal {
public:
Spaghetti( const Spaghetti & ); // copy ctor
void eat();
Spaghetti *clone() const
{ return new Spaghetti( *this ); } // call copy ctor
//...
};
const Meal *m = new Spaghetti;
Meal *myMeal = m->clone();
Item 30. Factory Method
哦,《设计模式》一书的更详细。
- Item 29. Virtual Constructors and Prototype&Item 30. Factory Method
- (Item 1) use static factory to substitute constructors
- Item 1: Consider static factory methods instead of constructors
- Effective Java——Item 1: Consider static factory methods instead of constructors
- Effective Java——Item 1,Consider static factory methods instead of constructors
- ITEM
- item
- item
- item
- item
- item
- Item Importer and Exporter
- item and material
- [设计模式]Factory Method和Prototype
- Chapter 22: Using Item Renderers and Item Editors--Creating an item renderer and item editor
- Chapter 22: Using Item Renderers and Item Editors--Creating drop-in item renderers and item editors
- Chapter 22: Using Item Renderers and Item Editors--Creating item renderers and item editor components
- Understanding Factory Method and Abstract Factory Patterns
- 我的J2ME编程练习(2)——DateField
- ACE使用经验:获得动态分配的监听端口号
- 【翻译】Microsoft SQL Server Passwords (Cracking the password hashes)&10个写出高质量WEB应用程序的技巧
- DirectX9 3D快速上手 3
- 简单外挂制作
- Item 29. Virtual Constructors and Prototype&Item 30. Factory Method
- 我,还是那个我;她还是那个她
- 关于JSP链接数据库的问题
- Item 31. Covariant Return Types & Item 32. Preventing Copying
- VB/JAVA与单片机通信
- Ant介绍
- 《Java 手机/PDA 程序设计入门》读书笔记11-- MIDP数据库设计1
- XML指南——XML CDATA
- Web新标准:Ajax 的一些资源汇总