zoukankan      html  css  js  c++  java
  • 设计模式 -- 装饰器

    装饰器模式的核心是有继承有组合,继承和组合的抽象类还是一个!

    用起来就是一层层的嵌套:

    BaseCharacter character = new Mage();
    character = new BaseDecorator(character);//
    character = new DecoratorHelmet(character);//
    character = new DecoratorShoulder(character);//
    character = new DecoratorBreastplate(caracter);
    character = new DecoratorCuish(character);
    character = new DecoratorGlove(character);

    能不能链式写呢?

     public class BaseDecorator : BaseCharacter
        {
            private BaseCharacter _BaseCharacter = null;
            public BaseDecorator(BaseCharacter character)
            {
                this._BaseCharacter = character;
            }
    
            public override void Show()
            {
                this._BaseCharacter.Show();
            }
            public BaseDecorator Decorat<T>() where T : BaseDecorator
            {
                return (T)Activator.CreateInstance(typeof(T), new object[] { this});
            }
        }

    这样就可以链式了!

     BaseCharacter character = new Mage();
    
     character = new BaseDecorator(character).Decorat<DecoratorHelmet>().Decorat<DecoratorCuish>();//
     character.Show();
    气功波(18037675651)
  • 相关阅读:
    多项式A除以B (25分)
    numpy随笔
    numpy中文件读取操作np.loadtxt(),np.savetxt()的使用
    Plug It In
    C. Uncle Bogdan and Country Happiness
    获得系统版本号
    C# Winform无边框窗口拖动
    numericUpDown隐藏上下箭头
    C# FTP下载图片转为Base64
    C# 获取版本号
  • 原文地址:https://www.cnblogs.com/qgbo/p/11516662.html
Copyright © 2011-2022 走看看