Patterns in SOME –Decorator

Code in C#:
 
namespace Decorator_DesignPattern
{
     using System;
 
     abstract class Component
     {
         public abstract void Draw();        
     }
 
     class ConcreteComponent : Component
     {
         private string strName;
         public ConcreteComponent(string s)
         {
              strName = s;          
         }
 
         public override void Draw()
         {
              Console.WriteLine("ConcreteComponent - {0}", strName);          
         }       
     }
 
     abstract class Decorator : Component
     {
         protected Component ActualComponent;
 
         public void SetComponent(Component c)
         {
              ActualComponent = c;
         }
         public override void Draw()
         {
              if (ActualComponent != null)
                   ActualComponent.Draw();    
         }
     }
 
     class ConcreteDecorator : Decorator
     {
         private string strDecoratorName;
         public ConcreteDecorator (string str)
         {
              // how decoration occurs is localized inside this decorator
              // For this demo, we simply print a decorator name
              strDecoratorName = str;
         }
         public override void Draw()
         {
              CustomDecoration();
              base.Draw();
         }
         void CustomDecoration()
         {
              Console.WriteLine("In ConcreteDecorator: decoration goes here");
              Console.WriteLine("{0}", strDecoratorName);
         }
     }
 
    
     ///<summary>
     ///    Summary description for Client.
     ///</summary>
     public class Client
     {
         Component Setup()
         {
              ConcreteComponent c = new ConcreteComponent("This is the real component");
 
              ConcreteDecorator d = new ConcreteDecorator("This is a decorator for the component");
 
              d.SetComponent(c);
 
              return d;
         }
        
         public static int Main(string[] args)
         {
              Client client = new Client();
              Component c = client.Setup();   
 
              // The code below will work equally well with the real component,
              // or a decorator for the component
 
              c.Draw();
             
              return 0;
         }
     }
}
 
Code in SOME:
  
AComponent
       a_Draw()
      
CConcreteComponent :AComponent
       (string _strName)
       o_Draw()
 
ADecorator : AComponent, ->AComponent[m_actualComponent]
       SetComponent(AComponent)
       o_Draw()
 
CConcreteDecorator : ADecorator
       (string _strDecoratorName)
       o_Draw()
       CustomDecoration()
 
CClient
       AComponent Setup()
       main
 
 
CClient.main
{
       CClient client.();
       AComponent c = client.Setup()
       {
                     CConcreteComponent c.("This is the real component");
                     CConcreteDecorator d.("This is a decorator for the component");
                     d.SetComponent( m_actualComponent = c );             //quick assignment for set method
                     d;                                //return d;
       };
       c.Draw<CConcreteDecorator>()
       {
              CustomDecoration();
              Draw<ADecorator>()                                                            //invoke base method
              {
                     m_actualComponent.Draw<CConcreteComponent>()
                     {
                                   <% Console.WriteLine("ConcreteComponent - {0}", _strName); %>
                     };
              };
       };
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值