Cosmetic mode (decorator structured) C # simple Example

Source: Internet
Author: User

Cosmetic mode (decorator structured) C # simple Example

The basic function of the player is to move, run and so on. Baseability
New additions: 1, damage skill harmability;2, obstruction skill baulkability;3, auxiliary skill assistability
Player 1 increased damage skill Decorator harm = new harmability (baseability);
Player 2 increases damage skill, hinders skill Decorator baulk = new baulkability (harm);
Player 3 adds damage skills, obstruction skills and assistive skills Decorator assist = new Assistability (baulk);

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778798081828384858687888990919293949596979899100 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 += "-----辅助-----";        }    }}

Cosmetic mode (decorator structured) C # simple Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.