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);


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

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.