欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#装饰者模式实例分析

程序员文章站 2023-12-19 10:42:58
本文实例讲述了c#装饰者模式。分享给大家供大家参考。具体方法如下: using system; using system.collections.generic...

本文实例讲述了c#装饰者模式。分享给大家供大家参考。具体方法如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;

namespace 装饰者
{
  class program
  {
    static void main(string[] args)
    {
      concretecomponent con = new concretecomponent();
      concretedecoratora a = new concretedecoratora();
      a.setcomponent(con);
      a.operation();
      a.addbehavior();
      console.writeline("============================");
      concretedecoratorb b = new concretedecoratorb();
      b.setcomponent(a);
      a.addbehavior();
      b.operation();
      b.addbehavior();
    }
    public abstract class component
    {
      public abstract void operation();
    }
    abstract class decorator : component
    {
      private component component;
      public void setcomponent(component component)
      {
        this.component = component;
      }
      public override void operation()
      {
        if (component != null)
        {
          component.operation();
        }
      }
    }
    class concretedecoratora : decorator
    {
      public override void operation()
      {
        base.operation();
      }
      public void addbehavior()
      {
        console.writeline("装饰a类的操作!");
      }
    }
    class concretedecoratorb : decorator
    {
      public override void operation()
      {
        base.operation();
      }
      public void addbehavior()
      {
        console.writeline("装饰b类的操作!");
      }
    }
    public class concretecomponent : component
    {
      public override void operation()
      {
        console.writeline("具体对象的操作!");
      }
    }
  }
}

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: