说说设计模式~装饰器模式(Decorator)

返回目录

装饰器模式,也叫又叫装饰者模式,顾名思义,将一个对象进行包裹,包装,让它变成一个比较满意的对象,这种模式在我们平时项目开发中,经常会用到,事实上,它是处理问题的一种技巧,也很好的扩展了程序,让程序代码不那么死板!

何时能用到它?

1. 需要扩展一个类的功能,或给一个类添加附加职责。

2. 需要动态的给一个对象添加功能,这些功能可以再动态的撤销。

3. 需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。

4. 当不能采用生成子类的方法进行扩充时。

其中我们认为第四种使用是比较巧妙的,这一讲中,主要是以第四讲为例来说明的

装饰器模式的结构图

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjIAAAFWCAIAAAD8I+DfAAAgAElEQVR4nO3dW3cT5933cb0n3sFz3PaoTTlr2kWcpr1bwgIa3zwOT1xScxeT4tR1inIn0CQlEAi7YDCrZAVC6oCDQlIgWEbeb/DeSJY1Y23seQ5GHsYzI2kkjTTXNfP9rP/KEvJYTKwf108z2jiiAQAgjIjfOwAAwHPUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSfDA/N3vr1o2e7s6e7s79+17Zs3sX0+TRf/i9V85PT0/6HQdgG2oJzfb5jWtHj7RfunAmkYiPjg+tZpbS6yvGrK0/s02yONnkWjaZ2TapUqNkV5XsqpKzT1of9fmsWSe/pubX1vOZUpPNK6VHNU9On8K6bbL65B0mV5yNXH4jV9g2+RJT2NBn05gNy2wWp2hqemJk5Il+X5w+dVJRMn7nAiiiltA883OzXcc6eq+cT2UWl9am5lIjphnVZ351bGvG9VlYnShOemIhPbGYnjTNlD5La/pMG7O8NrO8NrOcKc5K5unWzOrzTJnbmnljkspCUllIqgtJdSGlLppnVV1aVZdW15dW15fS68vbRy9UfbYVakafYlNulWV2Vcmm9bFX43ouUxyHClSzeTVXMOZ5yRUrbcMos22lVdhwrqvNzU1N02KxgaNH2sfHRvwOCKBp1BKaZnp68nBH28TEyNLa9GxqeDY1TC2JUEubm5ub2mYqlew61pFIxP2OCUAtoSnyuVzXsY7pmYn51OhscphaEq2WNrVNJZP505sHOJsH31FLaIbrfZev911eWpuaTQ5TS2LWkqZp9+9/87/R7sXFRb/zglCjltBw83OzR4+0JzOLT5MJaknkWtI0rae784svboyOjvqbGYQZtYSG0w+V5lfHqaUm11I2l1UUJZvLmmupsFHQry8UCvZaisUG3j3+13g8nkgk/I0NQotaQsP1dHcmhuNPkwnHWorYGLWk/7HmWtK/PbS1lM2uT09P9/X1LS0trWfXjVpaz64/evSor69PVdV8Pm+ppdXV1BsH98fj8Xg8PjU15W9yEE7UEhruUHvr4spsqVoyl5PlaMl9LelbcrRkriVFUR4/fvzSSy8dPXpUUZRcLruxWVBVdWRkZPfu3W1tbYqi5HI5Sy1pmrZn9674Fh9jg9CiltBw+/e9ksos1lxLz5uJWqryJJ6iKDdv3nzhhRc6OzuXl5cVRZmbm3v11Vf37t07MzOjqqr9JJ62vZbGx8d9TA7CiVpCw+3ZvWtVXa62lsyn9cy1ZL7e3EmWZjKusdSScf0zZc5y8jBgtZTNZefn5+/evdvX1/fiiy++/fbbd+/e/d3vfrdv376bN2/evXtXVdXi00ula4kDJjQftYSG87CWttpo0lxLpY6W7LWkX7OSmbU00zNlXr8QpFrST+L99re/fe+99+7cudPS0vLzn//81VdfvXv3bltb28GDB92cxKOW0HzUEhqu/loqNpOpltycxAt5LRU28plM5urVqz/+8Y/feeed/v7+N998M5lMvvbaay+//P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SAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg1BIAQCDUEgBAINQSAEAg/x9TBFvpbFD6CQAAAABJRU5ErkJggg==" alt="" />

装饰器模式实现说明

IAction:装饰器标准接口,所有装饰功能都要实现它

DelegateAction:装饰类,用来实现IAction插口的功能,并对外部提供另一种表现形式

StandardAction:标准实现类,用来实现IAction插口的功能,对外展示也是以IAction接口为准的

Implement:对外公开的调用类,它向外界公开两种接口方法,一是IAction接口标准,一是Action<int>委托标准

装饰器模式的C#实现

#region 装饰模式
public interface IAction
{
void Print(int a);
}
sealed class DelegateAction : IAction
{
Action<int> _action;
public DelegateAction(Action<int> action)
{
_action = action; }
public void Print(int a)
{
_action(a);
}
} public class standardAction : IAction
{
public void Print(int a)
{
Console.WriteLine("标准实现装饰器" + a);
}
}
public class Implement
{
public void Run(IAction action)
{
action.Print();
} public void Run(Action<int> action)
{
new DelegateAction(action).Print();
} }
#endregion

调用的代码

 Implement implement = new Implement();
implement.Run((a) => Console.WriteLine(a));//委托Action<int>调用法
implement.Run(new standardAction());//IAction对象调用法

返回目录

上一篇:USB协议分析


下一篇:iOS 3D Touch功能 3 -备