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);
namespace adapterpattern{public partial class Decoratorform:form {public Decoratorform () { InitializeComponent (); private void Btndisplay_click (object sender, EventArgs e) {baseability baseability = new Baseo Peration (); Baseability.run (); LISTBOX1.ITEMS.ADD (basedata.basestring); basedata.basestring = ""; LISTBOX1.ITEMS.ADD ("*********** Player 1 new features: Damage **************"); Decorator harm = new harmability (baseability); Harm. Run (); LISTBOX1.ITEMS.ADD (basedata.basestring); basedata.basestring = ""; LISTBOX1.ITEMS.ADD ("*********** Player 2 new features: Damage and obstruction **************"); Decorator baulk = new baulkability (harm); Baulk. Run (); LISTBOX1.ITEMS.ADD (basedata.basestring); basedata.basestring = ""; LISTBOX1.ITEMS.ADD ("*********** player 3 new features: damage, obstruction, auxiliary **************"); Decorator assist = new AssisTability (baulk); Assist. Run (); LISTBOX1.ITEMS.ADD (basedata.basestring); }} public abstract class Baseability//abstract basic function {public abstract void Run (); public class baseoperation:baseability//Entity Foundation function {public override void Run () {basedata. basestring + = "---mobile---"; Basedata.basestring + = "----running----"; }} public abstract class decorator:baseability//interface inherits {Private Baseability Baseability;//has-a object combination Public Decorator (baseability baseability)//Decorative connection point {this.baseability = baseability; } public override void Run () {baseability.run (); }} public class basedata//data broker {public static string basestring {get; set;} } public class harmability:decorator//add damage Skill {public harmability (baseability baseability): Bas E (baseability) {} public override void Run ()Add damage {//base. Move (); Base. Run (); Basedata.basestring + = "-----damage-----"; }} public class baulkability:decorator//add obstruction skill {public baulkability (baseability baseability) : Base (baseability) {} public override void Run ()//Add block {//base. Move (); Base. Run (); Basedata.basestring + = "-----obstruct-----"; }} public class assistability:decorator//add auxiliary skill {public assistability (baseability baseability) : Base (baseability) {} public override void Run () {base. Run (); Basedata.basestring + = "-----auxiliary-----"; } }}
Cosmetic mode (decorator structured) C # simple Example