Decorator pattern-C #3.0 Design Patterns

Source: Internet
Author: User

Decorator pattern belongs to Structural Patterns

Introduction:

The role of decorator pattern is to dynamically add new roles or States to an object. The decorated object is not "decorated.A key implementation point in the decorator pattern is that decorators both inherit the original class and contain an instantiation of it.

Implementation:

UML class diagram

     Public   Interface Icomponent { String Operation ();} /// <Summary>      /// Decorator      /// </Summary>      Public   Class Component: icomponent { # Region Icomponent members Public   String Operation (){ Return   "I'm a photo ;" ;} # Endregion } /// <Summary>      /// Modifier      /// </Summary>      Public   Class Decoratora: icomponent {icomponent component; Public Decoratora (icomponent component ){ This . Component = component ;} # Region Icomponent members Public   String Operation (){ Return Component. Operation () + "And decorated by decoratora" ;} # Endregion } /// <Summary>      /// Modifier B      /// </Summary>      Public   Class Decoratorb: icomponent {icomponent component; Public   String Addedstate = "And added state" ;Public Decoratorb (icomponent component ){ This . Component = component ;} # Region Icomponent members Public   String Operation (){ Return Component. Operation () + "And decorated by decoratorb" ;} Public   String Addedbehavior (){ Return   "And added behavior by decoratorb" ;} # Endregion } Public   Class Client { Public   Static   Void Display ( String S, icomponent component) {console. writeline (S + component. Operation () ;}} [testfixture] Public   Class Decoratortest {[test] Public   Void T1 () {var Component = New Component (); client. Display ( String . Empty, component); var decoratorb =New Decoratorb (component); client. Display ( "Hello! " , Decoratorb );}}

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.