码迷,mamicode.com
首页 > Windows程序 > 详细

C#装饰者模式实例代码

时间:2014-10-11 15:02:05      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:io   ar   sp   on   cti   代码   ad   line   new   

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#装饰者模式实例代码

标签:io   ar   sp   on   cti   代码   ad   line   new   

原文地址:http://www.cnblogs.com/gc2013/p/4018884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!