|
| 1 | +import Foundation |
| 2 | +/* |
| 3 | + Decorator es un patron de diseño estructural que permite añadir |
| 4 | + dinamicamente nuevos comportamientos a un objeto colocandolo |
| 5 | + dentro de objetos que los envuelven.(wrapper) |
| 6 | + |
| 7 | + para mas informacion https://refactoring.guru/es/design-patterns/decorator |
| 8 | + |
| 9 | + |
| 10 | + */ |
| 11 | + |
| 12 | + // 1.- creamos un protocolo(interface) para las operaciones que vamos a modificar |
| 13 | + protocol MacComputer{ |
| 14 | + func getDataComputer() -> String |
| 15 | + } |
| 16 | + |
| 17 | + //2.- creamos una clase que implemente el protocolo |
| 18 | + class MacbookPro: MacComputer{ |
| 19 | + func getDataComputer() -> String { |
| 20 | + return "MackbookPro has " |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + //3.- creamos la clase que va a ser el wrapper de la clase MacbookPro |
| 25 | + class MacDecorator: MacComputer{ |
| 26 | + private var macbookPro: MacComputer |
| 27 | + |
| 28 | + init(_ macbookPro: MacComputer){ |
| 29 | + self.macbookPro = macbookPro |
| 30 | + } |
| 31 | + func getDataComputer() -> String { |
| 32 | + return macbookPro.getDataComputer() |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + //4.-creamos los diferentes objetos que va envolver nuestro wrapper |
| 37 | + class MacDecoratorCpu: MacDecorator{ |
| 38 | + override func getDataComputer() -> String { |
| 39 | + return super.getDataComputer() + "M3 pro processor" |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + class MacDecoratorRam: MacDecorator{ |
| 44 | + override func getDataComputer() -> String { |
| 45 | + return super.getDataComputer() + " and 16 GB ram" |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + //5.- creamos una instancia de la clase MacDecorator |
| 50 | + let macbookPro = MacbookPro() |
| 51 | + let macDecoratorCpu = MacDecoratorCpu(macbookPro) |
| 52 | + let macDecoratorRam = MacDecoratorRam(macDecoratorCpu) |
| 53 | + print(macDecoratorRam.getDataComputer()) |
| 54 | + |
| 55 | +// reto extra |
| 56 | + |
| 57 | +protocol Count{ |
| 58 | + func getCount() -> Int |
| 59 | +} |
| 60 | + |
| 61 | +class CountExucution: Count{ |
| 62 | + private var count = 0 |
| 63 | + func getCount() -> Int { |
| 64 | + count += 1 |
| 65 | + return count |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class CountDecorator: Count{ |
| 70 | + private var countExucution: Count |
| 71 | + init(_ countExucution: Count){ |
| 72 | + self.countExucution = countExucution |
| 73 | + } |
| 74 | + func getCount() -> Int { |
| 75 | + return countExucution.getCount() |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +class SwiftVersion: CountDecorator{ |
| 80 | + |
| 81 | + func getSwiftVersion() -> String { |
| 82 | + print("function invoke \(getCount()) times") |
| 83 | + return "5.10.1" |
| 84 | + } |
| 85 | + |
| 86 | + override func getCount() -> Int { |
| 87 | + return super.getCount() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +let swiftVersion = SwiftVersion(CountExucution()) |
| 92 | +print(swiftVersion.getSwiftVersion()) |
| 93 | +print(swiftVersion.getSwiftVersion()) |
0 commit comments