惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
H
Help Net Security
量子位
The Cloudflare Blog
博客园 - Franky
博客园 - 聂微东
博客园 - 司徒正美
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
罗磊的独立博客
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
MongoDB | Blog
MongoDB | Blog

博客园 - Tristan(GuoZhijian)

Core Design Patterns(16) Chain of Responsibility 职责链模式 Core Design Patterns(15) Template Method 模版方法模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(13) Strategy 策略模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(9) Factory Method 工厂方法模式 Core Design Patterns(8) Prototype 原型模式 Core Design Patterns(7) Facade 外观模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(5) Flyweight 享元模式 Core Design Patterns(4) Composite 组合模式 Core Design Patterns(3) Bridge 桥接模式 Core Design Patterns(2) Proxy 代理模式 老调重弹:插件式框架开发的一个简单应用 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
Core Design Patterns(1) Decorator 装饰模式
Tristan(GuoZhijian) · 2008-02-24 · via 博客园 - Tristan(GuoZhijian)

VS 2008

当我们需要给一个已有的对象动态的附加状态或行为时,使用装饰模式。
装饰模式的要点是:装饰类实现了最初的被装饰类,并且包含了被装饰类的一个实例

1. 模式静态类图

2. 应用

    从一只可以写的Pen开始
    把它装饰成有颜色的笔、粗线的笔,甚至可以是有颜色且粗线的笔

IWritable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Decorator.BLL {
    
interface IWritable {
        
void Write();
    }

}

Pen

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Decorator.BLL {
    
class Pen : IWritable {
        
IWritable Members
    }

}

ColoredPen

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Decorator.BLL {
    
class ColoredPen : IWritable {

        IWritable component 
= null;
        
string color = string.Empty;

        
public ColoredPen(IWritable component, string color) {
            
this.component = component;
            
this.color = color;
        }


        
IWritable Members

        
public void SetColor(string color) {
            
this.color = color;
        }

    }

}

BoldPen

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Decorator.BLL {
    
class BoldPen : IWritable {

        IWritable component 
= null;
        
int borderWidth = 0;

        
public BoldPen(IWritable component, int borderWidth) {
            
this.component = component;
            
this.borderWidth = borderWidth;
        }


        
IWritable Members

        
public void SetBorderWidth(int borderWidth) {
            
this.borderWidth = borderWidth;
        }

    }

}

Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.Decorator.BLL;

namespace DesignPattern.Decorator {
    
class Program {
        
static void Main(string[] args) {
            IWritable p 
= new Pen();
            p.Write();
            Console.WriteLine();

            ColoredPen coloredPen 
= new ColoredPen(p, "red");
            coloredPen.Write();
            Console.WriteLine();

            BoldPen boldPen 
= new BoldPen(p, 2);
            boldPen.Write();
            Console.WriteLine();

            BoldPen coloredAndBoldPen 
= new BoldPen(coloredPen, 4);
            coloredAndBoldPen.Write();
        }

    }

}