forked from VamshiIITBHU14/DesignPatternsInSwift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10) Structural - Adapter Design Pattern.swift
62 lines (45 loc) · 1.65 KB
/
10) Structural - Adapter Design Pattern.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
import UIKit
//Suppose you have a TestBatsman class with fieldWell() and makeRuns()methods. And also a T20Batsman class with batAggressively() method. Let’s assume that you are short on T20Batsman objects and you would like to use TestBatsman objects in their place. TestBatsmen have some similar functionality but implement a different interface (they can bat but cannot bat in the way needed for a T20 match), so we can’t use them directly. So we will use adapter pattern. Here our client would be T20Batsman and adaptee would be TestBatsman.
protocol TestBatsman {
func makeRuns()
func fieldWell()
}
class Batsman1 : TestBatsman{
func makeRuns() {
print("I can bat well but only at StrikeRate of 80")
}
func fieldWell() {
print("I can field well")
}
}
protocol T20Batsman{
func batAggressively()
}
class Batsman2 : T20Batsman{
func batAggressively() {
print("I need to bat well at a StrikeRate of more than 130")
}
}
class TestBatsmanAdapter : T20Batsman{
let testBatsman : TestBatsman
init (_ testBatsman : TestBatsman){
self.testBatsman = testBatsman
}
func batAggressively() {
testBatsman.makeRuns()
}
}
func main(){
let batsman1 = Batsman1()
let batsman2 = Batsman2()
// Wrap a TestBatsman in a TestBatsmanAdapter so that he plays like T20Batsman
let testBatsmanAdapter = TestBatsmanAdapter(batsman1)
print("Test Batsman")
batsman1.fieldWell()
batsman1.makeRuns()
print("T20 Batsman")
batsman2.batAggressively()
print("TestBatsmanAdapter")
testBatsmanAdapter.batAggressively()
}
main()