设计模式c#语言描述——装饰(Decorator)模式

装饰模式又名包装模式,以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。它使用原来被装饰的类的一个子类的实例,把客户端的调用委派到被装饰类,客户端并不会觉得对象在装饰前和装饰后有什么不同。在以下情况下应使用装饰模式:需要扩展一个类的功能,或给一个类增加附加责任。动态地给一个对象增加功能,这些功能可以再动态地撤销。需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变得不现实。

?

类图如下所示:

?

?

装饰模式包括如下角色:

抽象构件(Component):给出一个抽象接口,以规范准备接收附加责任的对象。

具体构件(Concrete Component):定义一个将要接收附加责任的类。

装饰(Decorator):持有一个构件对象的实例,并定义一个与抽象构件接口一致的接口。

具体装饰(Concrete Decorator):负责给构件对象“贴上”附加的责任。

?

Component:

public interface Component

???? {

???????? void sampleOperation();

???? }// END INTERFACE DEFINITION Component

?

Decorator:

???? public? class Decorator : Component

???? {

???????? private Component component;

?

???????? public? Decorator(Component component)

???????? {

????????????? this.component=component;

???????? }

?

???????? public virtual void sampleOperation()

???????? {

????????????? component.sampleOperation();

???????? }

?

???? }// END CLASS DEFINITION Decorator

?

ConcreteComponent:

???? public class ConcreteComponent?? : Component

???? {

?

???????? public? void sampleOperation()

???????? {

???????? Console.WriteLine ("ConcreteComponent sampleOperation");

???????? }

?

???? }// END CLASS DEFINITION ConcreteComponent

?

ConcreteDecorator1:

???? public class ConcreteDecorator1? : Decorator

???? {

?

???????? public ConcreteDecorator1(Component component):base(component)

???????? {

?????????????

???????? }

???????? override public? void sampleOperation()

???????? {

???????? base.sampleOperation ();

???????? Console.WriteLine ("ConcreteDecorator1 sampleOperation");

???????? }

?

???? }// END CLASS DEFINITION ConcreteDecorator1

?

ConcreteDecorator2:

???? public class ConcreteDecorator2? : Decorator

???? {

???????? public ConcreteDecorator2 (Component component):base(component)

???????? {

???????? }

???????? override public void sampleOperation()

???????? {

????????????? base.sampleOperation ();

????????????? Console.WriteLine ("ConcreteDecorator2 sampleOperation");

???????? }

?

}// END CLASS DEFINITION ConcreteDecorator2

?

Client:

static void Main (string[] args)

???????? {

????????????? Component component=new ConcreteComponent ();

????????????? Component concretedecorator1=new ConcreteDecorator1 (component); // 包装

????????????? Component concretedecorator2=new ConcreteDecorator2 (concretedecorator1);

?

????????????? concretedecorator2.sampleOperation ();

???? }

?

程序输出如下:

ConcreteComponent sampleOperation

ConcreteDecorator1 sampleOperation

ConcreteDecorator2 sampleOperation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设计模式是一种被广泛应用于软件开发中的最佳实践方法,用于解决常见的设计问题。在C#中,有23种经典的设计模式,它们可以分为三个主要的分类:创建型模式(Creational Patterns)、结构型模式(Structural Patterns)和行为型模式(Behavioral Patterns)。下面是这些模式的简要介绍: 1. 创建型模式: - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 建造者模式(Builder Pattern) - 原型模式(Prototype Pattern) - 单例模式(Singleton Pattern) 2. 结构型模式: - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰模式Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式: - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 这些设计模式提供了一种通用的思路和结构,可以帮助开发人员更好地组织和设计他们的代码。具体的实现细节和用法可以根据具体的需求进行深入学习和研究。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值