-
Notifications
You must be signed in to change notification settings - Fork 0
/
Composite.swift
90 lines (55 loc) · 2.03 KB
/
Composite.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// Composite.swift
// Design Patterns
//
// Created by Samuel Esposito on 27/08/2019.
// Copyright © 2019 Samuel Esposito. All rights reserved.
//
import Foundation
/*Composite Pattern is a pattern that allows us to group objects in a hierarchical way, working with grouped objects may be more faster in some cases*/
/* STEP 1: Design a common interface for our objects*/
protocol Cleanable{
func clean(_ with: String)
}
/* STEP 2: create the single classes that implements the interface*/
class Glass: Cleanable{
func clean(_ with: String) {
print("the item was cleaned with \(with)")
}
}
class Dish: Cleanable{
func clean(_ with: String) {
print("the item was cleaned with \(with)")
}
}
/*STEP 3: create the group that implements the interface*/
class Dishwasher: Cleanable{
var cleanables = [Cleanable]()
func clean(_ with: String) {
for items in cleanables{
print("the current item was cleaned \(with)")
}
}
}
/*STEP 3: let's try on our main*/
func main(){
//Example 1: we can use the clean function on single items
let glass = Glass()
let dish = Dish()
dish.clean("soap") //print the single item
glass.clean("soap") //print the single item
let dishwasher = Dishwasher()
//Example 2: we can group the items and use the clean function
dishwasher.cleanables.append(dish)
dishwasher.cleanables.append(glass)
dishwasher.clean("soap") //print the items (2 items)
//Example 3: we can group groups and use the clean function
let anotherGlass = Glass()
let anotherDish = Dish()
let group = Dishwasher()
group.cleanables.append(anotherGlass)
group.cleanables.append(anotherDish)
dishwasher.cleanables.append(group)
dishwasher.clean("much more soap") //print the original items (2 items) + the group (1 items because the objects we added inside the group are counted like a single one object)
}
main()