Design Mode-decorator Mode

Source: Internet
Author: User

Design Mode-decorator Mode
Decorator is defined as a Decorator mode. Instead of adding or modifying class attributes in a derived class, the Decorator dynamically designs the Decorator mode to achieve this effect. It is more convenient and flexible than inheritance to reduce the complexity of the program. For example, Wang Feng created a champion team. First, the team category is empty. After constant efforts, Wang Feng strives for the team members and creates a suitable platform for them to play. The team is constantly becoming stronger and more complete. It is up to the decorator who adds and modifies the base class according to different needs, and finally wins your approval to meet your needs. Class Diagram for implementing the Assembly mode: copy the code of the team formation code // abstract class WangFengTeam of the Wang Feng team {// execute the planning command abstract public void Acrion ();} // Student class Student: wangFengTeam {// Methods override public void Acrion () {Console. writeLine ("Wang Feng team student situation:") ;}// team Chief Planning abstract class Direct: WangFengTeam {// Wang Feng team protected WangFengTeam team; // public void SetComponent (WangFengTeam team) {this. team = team;} // execution plan override public v Oid Acrion () {if (team! = Null) {team. Acrion () ;}}// the boy sang a wild expression and constructed a combination. Class BoyTeam: Direct {// combination name public string teamName = "Jquery"; // specific planning override public void Acrion () {base. acrion (); Console. writeLine ("I'm from the Wang Feng team, crazy. ") ;}/// The girl performs the class GrilTeam: Direct {// specific planning override public void Acrion () {base. acrion (); Console. writeLine ("I'm from the Wang Feng team, WAN Wei. "); // Show ();} public void show () {Console. writeLine ("graceful, catwalk") ;}copy the code client code: copy the code public static void Main () {Student team = new Student (); boyTeam boy = new BoyTeam (); GrilTeam girl = new GrilTeam (); // team boy decoration boy. setComponent (team); // team girl decoration girl. setComponent (boy); girl. acrion (); Console. read () ;}copy the code decorator mode to understand the function attributes of the dynamic extension class, without the need to inherit the hierarchy. This changes some of the limitations of the inheritance class, it is also more convenient and flexible to add responsibilities to the class. However, when using the decorator mode, it will also bring troubles to the maintenance personnel, because it will produce many small objects, causing you a headache. Therefore, the code comments, make sure that the code is well organized to prevent the maintenance personnel from getting stuck with the code.

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.