Skip to content

Commit 978006c

Browse files
committed
Strategy
1 parent a912b95 commit 978006c

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
protocol StrategyTextFormatter {
6+
func format(text: String)
7+
}
8+
9+
class CapitalStategyTextFormatter: StrategyTextFormatter {
10+
func format(text: String) {
11+
print("Texto en Mayusculas: \(text.uppercased())")
12+
}
13+
}
14+
15+
class LowerStategyTextFormatter: StrategyTextFormatter {
16+
func format(text: String) {
17+
print("Texto en Minusculas: \(text.lowercased())")
18+
}
19+
}
20+
21+
class Context {
22+
var strategyTextFormatter: StrategyTextFormatter
23+
24+
init(strategyTextFormatter: StrategyTextFormatter) {
25+
self.strategyTextFormatter = strategyTextFormatter
26+
}
27+
28+
func publishText(text: String) {
29+
strategyTextFormatter.format(text: text)
30+
}
31+
}
32+
33+
// TEST
34+
func testStrategy() {
35+
let contextCapital = Context(strategyTextFormatter: CapitalStategyTextFormatter())
36+
contextCapital.publishText(text: "este texto sera convertido a Mayusculas a traves de nuestro algoritmo")
37+
38+
let contextLower = Context(strategyTextFormatter: LowerStategyTextFormatter())
39+
contextLower.publishText(text: "este texto sera convertido a Minusculas a traves de nuestro algoritmo")
40+
}
41+
42+
testStrategy()
43+
44+
// RESULT
45+
///Texto en Mayusculas: ESTE TEXTO SERA CONVERTIDO A MAYUSCULAS A TRAVES DE NUESTRO ALGORITMO
46+
///Texto en Minusculas: este texto sera convertido a minusculas a traves de nuestro algoritmo
47+
48+
//: [Next](@next)

Design-Patterns-Swift.playground/contents.xcplayground

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
<page name='Facade'/>
1010
<page name='Proxy'/>
1111
<page name='Visitor'/>
12+
<page name='Strategy'/>
1213
</pages>
1314
</playground>

0 commit comments

Comments
 (0)