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