File tree 3 files changed +49
-0
lines changed
Design-Patterns-Swift.playground
Pages/Strategy.xcplaygroundpage
playground.xcworkspace/xcuserdata/josephestanislaocallamoreyra.xcuserdatad
3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
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)
Original file line number Diff line number Diff line change 9
9
<page name =' Facade' />
10
10
<page name =' Proxy' />
11
11
<page name =' Visitor' />
12
+ <page name =' Strategy' />
12
13
</pages >
13
14
</playground >
You can’t perform that action at this time.
0 commit comments