Swift design mode Learning-Decorative mode code Daquan

Source: Internet
Author: User

Decoration mode

The ability to dynamically extend an object without having to change the original class file and use inheritance. It is by creating a wrapper object, that is, decorating to wrap the real object.

Features of decoration mode
    1. Decorative objects and real objects have the same interface. This allows the client object to interact with the adornment object in the same way as the real object.
    2. Adornment object contains a reference to a real object (reference)
    3. The adornment object accepts all requests from the client. It forwards these requests to the real object.
    4. Decorative objects can add additional functionality before or after forwarding these requests. This ensures that additional functionality can be added externally without modifying the structure of a given object at run time. In object-oriented design, the extension of functionality to a given class is usually achieved through inheritance.
The basic realization of decoration mode

is the most basic decorative pattern of the structure diagram, the following will be implemented with the SWIFT code to achieve a basic decorative mode:

1). Write a component interface first

{    func operation()}
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

2). Write a specific Component object

class ConcreteComponent: Component {    func operation() {        // 具体操作的实现 }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

3). Write a decorative abstract class

class Decorator: Component {    var component: Component? func operation() { component?.operation() }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

4). Write Specific decorative objects

Decorative Object AClassconcretedecoratora: decorator { Private var addedstate:string? override func operation () {super.operation () AddedState = Span class= "hljs-string" and "Concretedecoratora unique features to differentiate Concretedecoratorb" // The operation of the specific decorative object a}}//decorative objects Bclass concretedecoratorb: decorator {override Func operation () {super.operation () Addedbehavior () // Specific Decorative object B operation} //concretedecoratorb unique method to distinguish Concretedecoratora private func Addedbehavior () {}}         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st

With the above code we can simply understand the basic implementation of the decorative pattern in the process, its main purpose is: without changing the original class file and using inheritance, the ability to dynamically extend an object , like the example code above, ConcreteDecoratorA and ConcreteDecoratorB to extend ConcreteComponent the The function of this object, so as not to change ConcreteComponent the source file of this class, can also achieve the effect of the extension.

Use decorative mode to solve practical problems (Buy car matching problem)

The following is a practical example to see, for example, when we buy a car is usually a lot of matching accessories, the following we through the strategy model to achieve this process, the next structure diagram:

Put the following code:

Componentprotocol Car {How much money func howmuch () IntAccessories for display assembly Func Showparts ()}ConcretecomponentsClassSuv:Car {init (owner:string) {print ("\ (owner) bought a suv,10w")} func Howmuch (), Int {Return() func showparts () {}}ClassMpv:Car {init (owner:string) {print ("\ (owner) bought a mpv,15w")} func Howmuch (), Int {Return} func Showparts () {}}DecoratorClassCarparts:Car {var car:car? Func Howmuch (), Int {Return car?. Howmuch ()??0} func Showparts () {car?. Showparts ()} func Decorator (_ Car:car), car {Self.car = carReturn self}}ConcretedecoratorsClassSofa:Carparts {Override Func Howmuch (), Int {ReturnSuper.howmuch () +1}Override Func Showparts () {Super.showparts () Print ("Optional leather sofa, 1W")}}Classsafety: carparts {override func Howmuch () Int {return super.howmuch () + 3} override func showparts () {super.showParts () Print ( "optional complete security system, 3W")}}class Span class= "Hljs-title" >engine: carparts {override func HowMuch () Int {return super.howmuch () + 5} override func showparts () { Super.showparts () print ( "optional V8 engine, 5W")}}       
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

Let's use the above code to see:

//WCL buy a Suv "WCL") //assembly Sofa SUV = Sofa (). Decorator (SUV ) //assembly engine SUV = engine (). Decorator (SUV) //assembly safety System SUV = Safety (). Decorator (SUV) suv.showparts () print ( "altogether spent \ (Suv.howmuch ()) W ") print (" \ n ") //WCL buy a car Mpvvar Mpv:car = MPV (owner: // Assembling engine MPV = engine (). Decorator (MPV) //assembly safety System MPV = Safety (). Decorator (MPV) mpv.showparts () Span class= "Hljs-keyword" >print ( "altogether spent \ (Mpv.howmuch ()) W")     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

The following is the result of the code printing:

wcl买了一辆Suv,10W选配了真皮沙发,1W选配了V8发动机,5W选配了全套安全系统,3W一共花了19Wwcl买了一辆Mpv,15W选配了V8发动机,5W选配了全套安全系统,3W一共花了23W
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

In this way, it is easy to use the decorating mode to implement this process and operate it without changing the Suv Mpv source files of the two classes.

Summarize

Let's take a look at the advantages of decorating mode:

    1. You can remove the decorations in the class from the class to simplify the original class
    2. Effectively distinguishes between core duties and decorative functions, and eliminates repetitive decorative logic in related classes

Disadvantages of decorating mode:

    1. Although the decorative mode is more scalable, the number of classes is a little, how to choose the scalability and simplicity is a problem, there is a choice to sacrifice
    2. It's hard to figure out how many layers a class has been decorated with, maybe 1 or 100 stories.

Swift design mode Learning-Decorative mode code Daquan

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.