SharpDevelop设计模式(Decorator)

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

namespace  SharpDevelop设计模式.类库
{
    
///   <summary>
    
///  Decorator模式在运行时为对象添加功能,它从一个接口继承而来,并扩展
    
///  和实现了该接口的全部实现和方法,它接收在构造函数中实现该接口 一个对象
    
///  并将原始接口提供的所有调用委托给通过该构造函数接收的对象
    
///  decorator可以添加许多原始接口不包含的函数,从而就可以快速地添加功能
    
///  
    
///  如下代码是以Factory模式下来示例Decorator模式
    
///   </summary>
     public   interface  IHelloPrinterDecorator :IHelloPrinter
    {
        
// 扩展功能
         void  PrintGoodbye();
    }

    
public   abstract   class  AbstractHelloPrinterDecorator : IHelloPrinterDecorator
    {
        IHelloPrinter helloPrinter;
        
public  AbstractHelloPrinterDecorator(IHelloPrinter helloPrinter)
        {
            
this .helloPrinter  =  helloPrinter;
        }

        
public   void  PrintHello()
        {
            helloPrinter.PrintHello();
        }
        
public   abstract   void  PrintGoodbye();
    }

    
public   class  EnlishHelloPrinterDecorator : AbstractHelloPrinterDecorator
    {
        
public  EnlishHelloPrinterDecorator(IHelloPrinter helloPrinter)
            : 
base (helloPrinter)
        {

        }
        
public   override   void  PrintGoodbye()
        {
            System.Console.WriteLine(
" Good bye " );
        }
    }

    
public   class  ChineseHelloPrinterDecorator : AbstractHelloPrinterDecorator
    {
        
public  ChineseHelloPrinterDecorator(IHelloPrinter helloPrinter)
            : 
base (helloPrinter)
        {

        }
        
public   override   void  PrintGoodbye()
        {
            System.Console.WriteLine(
" 再见 " );
        }
    }
    
// 这里也使Decorator从抽象基类派生,但Decorator代码比HelloPrinter类更稳定
    
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值