设计模式:Inheritance(继承) 和 Delegation(委托)
2014-06-23 10:27
661 查看
设计模式:Inheritance(继承) 和 Delegation(委托)
今天继续讲Design Pattern的内容。所谓 Design Pattern,翻译过来就是设计模式,是OO语言的一些基本运用。今天介绍Design Pattern中的两个基本概念,Inheritance(继承) 和 Delegation(委托)。
先定义一个位图类:
class CBitmap
{
...
Private:
int height;
public:
int GetBitmapHeight(void) { return Iheight; }
}
所谓Inheritance就是继承,我想学过C++的人都知道什么是继承。以上面的CBitmap为例子,如果我们想生成一个CTexture类(Texture:纹理之意),并且保留CBitmap的功能,比如GetBitmapHeight。可以这么做:
class CTexture : public CBitmap
{
public:
CTexture();
~CTexture();
};
当时还有另外一种方法,并不使用继承,而是把CBitmap当做CTexture的一个成员,这就是Delegation。代码如下:
class CTexture
{
...
private:
CBitmap InternalBitmap;
public:
int GetBitmapHeight(void) { return InternalBitmap.GetBitmapHeight(); }
};
参考:/article/1744563.html
今天继续讲Design Pattern的内容。所谓 Design Pattern,翻译过来就是设计模式,是OO语言的一些基本运用。今天介绍Design Pattern中的两个基本概念,Inheritance(继承) 和 Delegation(委托)。
先定义一个位图类:
class CBitmap
{
...
Private:
int height;
public:
int GetBitmapHeight(void) { return Iheight; }
}
所谓Inheritance就是继承,我想学过C++的人都知道什么是继承。以上面的CBitmap为例子,如果我们想生成一个CTexture类(Texture:纹理之意),并且保留CBitmap的功能,比如GetBitmapHeight。可以这么做:
class CTexture : public CBitmap
{
public:
CTexture();
~CTexture();
};
当时还有另外一种方法,并不使用继承,而是把CBitmap当做CTexture的一个成员,这就是Delegation。代码如下:
class CTexture
{
...
private:
CBitmap InternalBitmap;
public:
int GetBitmapHeight(void) { return InternalBitmap.GetBitmapHeight(); }
};
参考:/article/1744563.html
相关文章推荐
- php设计模式 Delegation(委托模式)
- 处理概括关系之十二 :Replace Delegation with Inheritance(以继承取代委托)
- Replace Inheritance with Delegation(以继承取代委托)
- 11.11 replace inheritance with delegation(以委托取代继承)
- 【设计模式】--TEMPLATE METHOD 模式 和 STRATEGY 模式 :继承和委托
- Javascript 设计模式学习笔记(2) - 继承(Inheritance) (下)
- 重构第8天:使用委托代替继承(Replace Inheritance with Delegation)
- Replace Delegation with Inheritance (以继承取代委托)
- 重构手法68:Replace Delegation with Inheritance (以继承取代委托)
- 处理概括关系之十一 :Replace Inheritance with Delegation(以委托取代继承)
- 设计模式:Interface Inheritance (接口继承)和 Implementation Inheritance(实现继承)
- iOS开发之设计模式(三)委托模式(Delegation)
- c++面向对象之复合(composition)、委托(delegation)、继承(inheritance)--(boolan)
- Replace Inheritance with Delegation (以委托取代继承)
- 设计模式之Inheritance versus Parameterized Types 继承和参数化类型
- php设计模式 Delegation(委托模式)
- Javascript 设计模式学习笔记(2) - 继承(Inheritance) (上)
- [php]php设计模式 Delegation(委托模式)
- 重构手法67:Replace Inheritance with Delegation (以委托取代继承)
- Replace Delegation with Inheritance(以委托取代继承)