C#装饰者模式实例代码_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C#装饰者模式实例代码

C#装饰者模式实例代码

 2014/10/11 15:24:32  GC2013  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespace装饰者{classProgram{staticvoidMain(string[]args){ConcreteComponentcon=newConcreteComponent();ConcreteDecoratorAa=newConcreteDecoratorA();a.SetComponent(con);a
  • 标签:C# 代码 模式 实例
class="FocusMe">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("具体对象的操作!");
            }
        }
    }
}
发表评论
用户名: 匿名