关闭

大话设计模式之装饰模式

497人阅读 评论(0) 收藏 举报

敲了下代码,不是很理解

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcreteComponenth c = new ConcreteComponenth();
            ConcreteDecoratorA ca = new ConcreteDecoratorA();
            ConcreteDecoratorB cb = new ConcreteDecoratorB();
            ca.SetComponent(c);
            cb.SetComponent(ca);
            cb.Operation();
            Console.ReadLine();
        }
    }
    public abstract class Componenth{
        public abstract void Operation();
    }
    public class ConcreteComponenth:Componenth{
        public override void Operation()
        {
            Console.WriteLine("Concrete Operatiion");
        }
    }
    public abstract class Decorator:Componenth
    {
        protected Componenth component;
        public void SetComponent(Componenth _com)
        {
            this.component = _com;
        }
        public override void Operation()
        {
            if (component!=null)
            {
                component.Operation();
            }
            //throw new NotImplementedException();
        }
    }
    public class ConcreteDecoratorA:Decorator{
        private string addedState;
        public override void Operation()
        {
            base.Operation();
            addedState = "new state";
            Console.WriteLine("具体装饰对象A操作!");
        }
    }
    public class ConcreteDecoratorB:Decorator{
        public override void Operation()
        {
            base.Operation();
            AddedBehavior();
            Console.WriteLine("B operation");
        }

        void AddedBehavior()
        {

        }
    }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7439次
    • 积分:199
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条