装饰模式

Posted zhuyapeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了装饰模式相关的知识,希望对你有一定的参考价值。

装饰模式:

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

namespace 装饰模式

    class Program
    
        static void Main(string[] args)
        
            ConcreteComponent c = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

            d1.SetComponent(c);
            d2.SetComponent(d1);

            d2.Operation();

            Console.Read();
        
    

    abstract class Component
    
        public abstract void Operation();
    

    class ConcreteComponent : Component
    
        public override void Operation()
        
            Console.WriteLine("具体对象的操作");
        
    

    abstract class Decorator : Component
    
        protected Component component;

        public void SetComponent(Component component)
        
            this.component = component;
        

        public override void Operation()
        
            if (component != null)
            
                component.Operation();
            
        
    

    class ConcreteDecoratorA : Decorator
    
        private string addedState;

        public override void Operation()
        
            base.Operation();
            addedState = "New State";
            Console.WriteLine("具体装饰对象A的操作");
        
    

    class ConcreteDecoratorB : Decorator
    

        public override void Operation()
        
            base.Operation();
            AddedBehavior();
            Console.WriteLine("具体装饰对象B的操作");
        

        private void AddedBehavior()
        

        
    

 

以上是关于装饰模式的主要内容,如果未能解决你的问题,请参考以下文章

装饰模式(Decorator Pattern)

装饰器模式

设计模式之-装饰器模式

23设计模式装饰模式

设计模式---装饰者模式

设计模式之装饰模式