Design mode-(14) Decorator mode (Swift version)

Source: Internet
Author: User

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)

Related Article

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.