Decorator schema C # simple example
Decorator schema C # simple example
The basic functions of players are mobile and running. BaseAbility
New features: 1. Damage skill harmAbility; 2. Barrier skill BaulkAbility; 3. Auxiliary skill restorability
PLAYER 1 increases the damage skill Decorator harm = new HarmAbility (baseAbility );
PLAYER 2 increases the damage skill and obstacle skill. Decorator baulk = new BaulkAbility (harm );
Player 3: Increase the damage skill, obstacle skill, and auxiliary skill Decorator assist = new damage ability (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 ("************ Player 1 new function: 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, and assistance **************"); decorator assist = new configurability (baulk); assist. run (); listBox1.Items. add (baseData. baseString) ;}} public abstract class BaseAbility // basic abstract functions {public abstract void Run ();} public class BaseOperation: baseAbility // basic object functions {public override void Run () {baseData. baseString + = "--- move ---"; baseData. baseString + = "---- run ----" ;}} public abstract class Decorator: BaseAbility // interface inheritance {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 transfer station {public static string baseString {get; set ;}} public class HarmAbility: decorator // increase the damage skill {public HarmAbility (BaseAbility baseAbility): base (baseAbility) {} public override void Run () // increase the damage {// base. move (); base. run (); baseData. baseString + = "----- damage -----" ;}} public class BaulkAbility: Decorator // increase the barrier skill {public BaulkAbility (BaseAbility): base (baseAbility) {} public override void Run () // Add obstacles {// base. move (); base. run (); baseData. baseString + = "----- -----" ;}} public class AssistAbility: Decorator // Add auxiliary skills {public AssistAbility (BaseAbility): base (baseAbility) {} public override void Run () {base. run (); baseData. baseString + = "----- auxiliary -----";}}}