One, the concept
Decorator Mode (Decorator): Dynamically adds some additional responsibilities to an object, and to extend the functionality of an object, adorners provide a more resilient alternative than inheritance.
Multiple combinations, less inheritance
Two, UML diagram
Abstract Component Class (Component): An abstract interface is given to standardize the objects that are ready to receive additional responsibilities
Concrete Component Class (Concretecomponent): Defines a specific class that is prepared to accept additional responsibilities, which must implement the component interface.
Decorator Class (Decorator): Holds an instance of a component (Conponent) object and defines an interface that is consistent with the abstract component.
Specific decorator Class (concrete Decoratator): Defines the "affixed" additional responsibility to the Component object.
Third, use case
Protocol Component { func display (), Void;} Class Componentdecorator:component { var component:component var border:string? Init (component:component) { self.component = component } func display () { component.display () } }
Class Listview:component { var name:string = "ListView" func display () { print (name) }}class boxview:component { var name:string = "Boxview" func display () { print (name) }}
Class Blackboder:componentdecorator { override init (component:component) { super.init (component:component) border = "Black" } Override func display () { setborder () super.display () } func SetBorder () { print ("This border is \ (Border?? "Default") } }class yellowboder:componentdecorator { override init (component:component) { Super.init (component:component) border = "Yellow" } override func display () { setborder () Super.display () } func SetBorder () { print ("This border is \ (border??") Default ") } }
Client side:
Class Viewcontroller:uiviewcontroller { override func Viewdidload () { super.viewdidload () let component = ListView () let componentb = Yellowboder (component:component) componentb.display ()} }
Design mode-(14) Decorator mode (Swift version)