Decorator schema C # simple example

Source: Internet
Author: User

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 -----";}}}


Related Article

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.