@@ -326,7 +326,73 @@ tuple.z
326
326
```
327
327
328
328
##🚧 Bridge
329
- ##🚧 Decorator
329
+ ##🍧 Decorator
330
+
331
+ ``` swift
332
+ protocol Coffee {
333
+ func getCost () -> Double
334
+ func getIngredients () -> String
335
+ }
336
+
337
+ class SimpleCoffee : Coffee {
338
+ func getCost () -> Double {
339
+ return 1.0
340
+ }
341
+ func getIngredients () -> String {
342
+ return " Coffee"
343
+ }
344
+ }
345
+
346
+ class CoffeeDecorator : Coffee {
347
+ private let decoratedCoffee: Coffee
348
+ private let ingredientSeparator: String = " , "
349
+
350
+ required init (decoratedCoffee : Coffee) {
351
+ self .decoratedCoffee = decoratedCoffee
352
+ }
353
+ func getCost () -> Double {
354
+ return decoratedCoffee.getCost ()
355
+ }
356
+ func getIngredients () -> String {
357
+ return decoratedCoffee.getIngredients ()
358
+ }
359
+ }
360
+
361
+ class Milk : CoffeeDecorator {
362
+ required init (decoratedCoffee : Coffee) {
363
+ super .init (decoratedCoffee : decoratedCoffee)
364
+ }
365
+ override func getCost () -> Double {
366
+ return super .getCost () + 0.5
367
+ }
368
+ override func getIngredients () -> String {
369
+ return super .getIngredients () + ingredientSeparator + " Milk"
370
+ }
371
+ }
372
+
373
+ class WhipCoffee : CoffeeDecorator {
374
+ required init (decoratedCoffee : Coffee) {
375
+ super .init (decoratedCoffee : decoratedCoffee)
376
+ }
377
+ override func getCost () -> Double {
378
+ return super .getCost () + 0.7
379
+ }
380
+ override func getIngredients () -> String {
381
+ return super .getIngredients () + ingredientSeparator + " Whip"
382
+ }
383
+ }
384
+ ```
385
+
386
+ ** Usage:**
387
+ ``` swift
388
+ var someCoffee: Coffee = SimpleCoffee ()
389
+ println (" Cost : \( someCoffee.getCost () ) ; Ingredients: \( someCoffee.getIngredients () ) " )
390
+ someCoffee = Milk (decoratedCoffee : someCoffee)
391
+ println (" Cost : \( someCoffee.getCost () ) ; Ingredients: \( someCoffee.getIngredients () ) " )
392
+ someCoffee = WhipCoffee (decoratedCoffee : someCoffee)
393
+ println (" Cost : \( someCoffee.getCost () ) ; Ingredients: \( someCoffee.getIngredients () ) " )
394
+ ```
395
+
330
396
##🚧 Proxy
331
397
332
398
#Behavioral
0 commit comments