• 装饰模式


    装饰模式:

    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()
            {
    
            }
        }
    }
  • 相关阅读:
    天兔监控系统安装
    day6
    day5
    day4
    day3
    day2
    day1
    几个重要的Xenomai相关链接
    树莓派GPIO中断驱动程序
    转了一圈,再读LKD
  • 原文地址:https://www.cnblogs.com/zhuyapeng/p/11277861.html
Copyright © 2020-2023  润新知