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

推荐订阅源

博客园 - 三生石上(FineUI控件)
U
Unit 42
人人都是产品经理
人人都是产品经理
罗磊的独立博客
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
GbyAI
GbyAI
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
B
Blog RSS Feed
WordPress大学
WordPress大学
腾讯CDC
H
Help Net Security
博客园 - Franky
博客园 - 【当耐特】
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
B
Blog
Vercel News
Vercel News
博客园 - 司徒正美

博客园 - 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();
        }

    }

}