还是那几句话:

学无止境,精益求精

十年河东,十年河西,莫欺少年穷

学历代表你的过去,能力代表你的现在,学习代表你的将来

废话不多说,直接进入正题:

今天学习了装饰模式,但是代码看不太懂,于是我将装饰模式的代码改编成了如下代码:

如下:

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

namespace SJMS
{
    public abstract class Component
    {
        public abstract void Operation();
    }


    public abstract class Deaorator : Component
    {

        public void SetComponent(Component component)
        {
            component.Operation();

        }
    }

    public class ConcreteDeaoratorC : Deaorator
    {

        public override void Operation()
        {
            Console.WriteLine("具体装饰对象C的操作");
        }
    }

    public class ConcreteDeaoratorA : Deaorator
    {
        private string addedState;

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

    public class ConcreteDeaoratorB : Deaorator
    {
        public override void Operation()
        {
            AddedBehavior();
            Console.WriteLine("具体装饰对象B的操作");
        }

        private void AddedBehavior()
        {

        }
    }
}
View Code

相关文章:

  • 2022-01-05
  • 2022-12-23
  • 2021-12-01
  • 2021-03-30
  • 2022-01-15
  • 2021-06-28
猜你喜欢
  • 2021-08-25
  • 2022-12-23
  • 2021-05-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-30
相关资源
相似解决方案