• 修饰模式(Decorator结构化)C#简单的例子


    修饰模式(Decorator结构化)C#简单的例子

    播放器的基本功能是移动、执行等。BaseAbility
    新增功能:1、伤害技能harmAbility;2、阻碍技能BaulkAbility;3、辅助技能assistAbility
    玩家1添加核弹                                          Decorator harm = new HarmAbility(baseAbility);
    玩家2添加伤害技能、阻碍技能                      Decorator baulk = new BaulkAbility(harm);
    玩家3添加伤害技能、阻碍技能和辅助技能  Decorator assist = new AssistAbility(baulk);

    namespace adapterpattern
    {
        public partial class DecoratorForm : Form
        {
            public DecoratorForm()
            {
                InitializeComponent();
            }
    
            private void btnDisplay_Click(object sender, EventArgs e)
            {
                BaseAbility baseAbility = new BaseOperation();
                baseAbility.Run();
                listBox1.Items.Add(baseData.baseString);
    
                baseData.baseString = "";
                listBox1.Items.Add("***********玩家1新功能:伤害**************");
                Decorator harm = new HarmAbility(baseAbility);
                harm.Run();
                listBox1.Items.Add(baseData.baseString);
    
                baseData.baseString = "";
                listBox1.Items.Add("***********玩家2新功能:伤害和阻碍**************");
                Decorator baulk = new BaulkAbility(harm);
                baulk.Run();
                listBox1.Items.Add(baseData.baseString);
    
                baseData.baseString = "";
                listBox1.Items.Add("***********玩家3新功能:伤害、阻碍、辅助**************");
                Decorator assist = new AssistAbility(baulk);
                assist.Run();
                listBox1.Items.Add(baseData.baseString);
            }
        }
    
        public abstract class BaseAbility    //抽象的基础功能
        {
            public abstract void Run();
        }
        public class BaseOperation : BaseAbility//实体基础功能
        {
            public override void Run()
            {
                baseData.baseString += "---移动---";
                baseData.baseString += "----执行----";
            }
        }
        public abstract class Decorator : BaseAbility//接口继承
        {
            private BaseAbility baseAbility;//Has—A对象组合
            public Decorator(BaseAbility baseAbility)//装饰连接点
            {
                this.baseAbility = baseAbility;
            }
            public override void Run()
            {
                baseAbility.Run();
            }
        }
        public class baseData//数据中转站
        {
            public static string baseString { get; set; }
        }
        public class HarmAbility : Decorator//添加伤害技能
        {
            public HarmAbility(BaseAbility baseAbility)
                : base(baseAbility)
            { }
            public override void Run()//添加伤害
            {
                // base.Move();
                base.Run();
                baseData.baseString += "-----伤害-----";
            }
        }
        public class BaulkAbility : Decorator//添加阻碍技能
        {
    
            public BaulkAbility(BaseAbility baseAbility)
                : base(baseAbility)
            { }
            public override void Run()//添加阻碍
            {
                //   base.Move();
                base.Run();
                baseData.baseString += "-----阻碍-----";
            }
        }
        public class AssistAbility : Decorator//添加辅助技能
        {
            public AssistAbility(BaseAbility baseAbility)
                : base(baseAbility)
            { }
            public override void Run()
            {
                base.Run();
                baseData.baseString += "-----辅助-----";
            }
        }
    }
    

    此文件由朱朱编写,转载请注明出自朱朱家园http://blog.csdn.net/zhgl7688



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    浅谈线段树
    并查集最简单讲解
    mysql索引原理深度解析
    算法、数据结构可视化
    php算法题---对称的二叉树
    php算法题---连续子数组的最大和
    Jsoup一个简短的引论——采用Java抓取网页数据
    宏定义详细信息
    解决java.sql.SQLException: ORA-01789: query block has incorrect number of result columns
    BZOJ 2435 NOI2011 道路建设 BFS/DFS
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4891799.html
Copyright © 2020-2023  润新知