程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 修飾模式(Decorator結構型)C#簡單例子

修飾模式(Decorator結構型)C#簡單例子

編輯:C#入門知識

修飾模式(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 += "-----輔助-----";
        }
    }
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved