Skip to content

Commit 98470d6

Browse files
authored
Add files via upload
1 parent da232bb commit 98470d6

40 files changed

+414
-0
lines changed

FactoryEx.playground/Contents.swift

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let factory = PhoneFactory()
2+
3+
let phones: [Phone?] = [
4+
factory.createPhone("iPhone"),
5+
factory.createPhone("Galaxy"),
6+
factory.createPhone("Huawei"),
7+
factory.createPhone("LG"),
8+
factory.createPhone("Pixel"),
9+
factory.createPhone("NEXUS"),
10+
]
11+
12+
for p in phones {
13+
p?.call()
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Galaxy: Phone {
2+
3+
public override func call(){
4+
print("Galaxy makes a call")
5+
}
6+
}

FactoryEx.playground/Sources/LG.swift

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class LG: Phone {
2+
3+
public override func call(){
4+
print("LG makes a call")
5+
}
6+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Nexus: Phone {
2+
3+
public override func call(){
4+
print("Nexus makes a call")
5+
}
6+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public class Phone {
2+
public func call(){}
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class PhoneFactory {
2+
public init(){}
3+
4+
public func createPhone(_ type: String) -> Phone? {
5+
switch type.lowercased() {
6+
//supported types
7+
case "galaxy": return Galaxy()
8+
case "iphone": return iPhone()
9+
case "pixel": return Pixel()
10+
case "nexus": return Nexus()
11+
case "lg": return LG()
12+
//unsupported type
13+
default: return nil
14+
}
15+
}
16+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Pixel: Phone {
2+
3+
public override func call(){
4+
print("Pixel makes a call")
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class iPhone: Phone {
2+
3+
public override func call(){
4+
print("iPhone makes a call")
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

MyFactory.playground/Contents.swift

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let factory = CarFactory()
2+
3+
let cars: [Car] = [
4+
factory.createCar("Bus")!,
5+
factory.createCar("bus")!,
6+
factory.createCar("car")!,
7+
factory.createCar("PRIVATE")!,
8+
factory.createCar("truck")!
9+
]
10+
11+
for car in cars {
12+
car.drive() //polimorphic call
13+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Bus: Car {
2+
public override func drive() {
3+
print("Bus is driving")
4+
}
5+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Car {
2+
public func drive(){
3+
//basic usage
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class CarFactory {
2+
public init(){}
3+
4+
public func createCar(_ type: String) -> Car? {
5+
switch type.lowercased() {
6+
//supported types
7+
case "private","car": return Private()
8+
case "bus" : return Bus()
9+
case "truck" : return Truck()
10+
//unsupportd type
11+
default : return nil
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Private: Car {
2+
public override func drive() {
3+
print("Private car is driving")
4+
}
5+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Truck: Car {
2+
public override func drive() {
3+
print("Truck is driving")
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

MyMediator.playground/Contents.swift

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let mediator = Mediator()
2+
var a1 = Aircraft("F16", mediator)
3+
var a2 = Aircraft("F35", mediator)
4+
var a3 = Aircraft("Stealth", mediator)
5+
var a4 = Aircraft("Jumbo", mediator)
6+
7+
a1.send(msg: "hello")
8+
a3.send(msg: "FDASM$%#WV CX")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Aircraft {
2+
private let name: String
3+
private let mediator: Mediator
4+
5+
public init(_ name: String, _ mediator: Mediator) {
6+
self.name = name
7+
self.mediator = mediator
8+
self.mediator.register(aircraft: self)
9+
}
10+
11+
public func send(msg: String) {
12+
print("\(name) sent: \(msg)")
13+
self.mediator.publish(msg: msg, sender: self)
14+
}
15+
16+
public func receive(_ message: String) {
17+
print("\(name) received message: \(message)")
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Mediator {
2+
private var aircrafts: [Aircraft] = []
3+
4+
public init(){}
5+
6+
public func register(aircraft: Aircraft) {
7+
aircrafts.append(aircraft)
8+
}
9+
10+
public func publish(msg: String, sender: Any){
11+
for ac in aircrafts {
12+
if ac !== sender as? Aircraft {
13+
ac.receive(msg)
14+
}
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

MySingy.playground/Contents.swift

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//Tests:
2+
3+
var s1 = Singy.getInstance()
4+
var s2 = Singy.getInstance()
5+
6+
print(s1 === s2)
7+
s2.name = "Singy B"
8+
print(s1.name)
9+
10+
var s3 = Singy2.instance
11+
var s4 = Singy2.instance
12+
13+
print(s3 === s4)
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Singy {
2+
//instance variable
3+
public var name = "Singy A"
4+
5+
//stored Singy class instance
6+
private static var instance: Singy?
7+
8+
//to prevent instantiation from outside
9+
private init() {}
10+
11+
//not thread safe
12+
public static func getInstance() -> Singy {
13+
//if not existsfrom
14+
if Singy.instance == nil {
15+
//create & store statically
16+
Singy.instance = Singy()
17+
}
18+
19+
//return to caller
20+
return Singy.instance!
21+
}
22+
23+
//with property
24+
// public static var Instance : Singy {
25+
// if Singy.instance == nil {
26+
// //create & store statically
27+
// Singy.instance = Singy()
28+
// }
29+
//
30+
// //return to caller
31+
// return Singy.instance!
32+
// }
33+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Singy2 {
2+
//accessible stored Singy instance
3+
public static let instance = Singy2()
4+
//prevent instantiation from outside
5+
private init() {}
6+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

MyStrategy.playground/Contents.swift

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//usage
2+
let w1 = Warrior()
3+
4+
//press X - engage with warrior
5+
w1.engage()
6+
7+
//upgrade ability
8+
w1.setAbility(Shooting())
9+
10+
//press X - engage again
11+
w1.engage()
12+
13+
//another upgrade
14+
w1.setAbility(Kicking())
15+
16+
//Press X again
17+
w1.engage()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public protocol FightAbility {
2+
//Fight ability is to be able to attack
3+
func attack()
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Warrior {
2+
//Warrior HAS an ability..
3+
private var ability: FightAbility!
4+
5+
public init(){
6+
//default ability
7+
ability = Boxing()
8+
}
9+
10+
//use current ability
11+
public func engage(){
12+
self.ability.attack()
13+
}
14+
15+
//changing ability at runtime
16+
public func setAbility(_ ability: FightAbility){
17+
self.ability = ability
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Boxing: FightAbility {
2+
public init(){}
3+
4+
public func attack() {
5+
print("Sucker punch")
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class Kicking: FightAbility {
2+
public init(){}
3+
4+
public func attack() {
5+
print("Kick some ass")
6+
}
7+
}
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Shooting: FightAbility {
2+
public init(){}
3+
4+
public func attack() {
5+
print("Shoot to kill")
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

WarriorEx.playground/Contents.swift

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let mediator = Mediator()
2+
3+
let w1 = Warrior("Gidon", mediator)
4+
let w2 = Warrior("Tzahi", mediator)
5+
let w3 = Warrior("Idan", mediator)
6+
let w4 = Warrior("Matan", mediator)
7+
let w5 = Warrior("Netanel", mediator)
8+
let w6 = Warrior("Zohar", mediator)
9+
10+
w4.set(ability: IONCannon.instance)
11+
w5.send(msg: "what your status?")
12+
w1.set(ability: Shooting())
13+
14+
w6.callTeamAttack()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public protocol FightAbility {
2+
func attack()
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Mediator {
2+
private var warriors:[Warrior] = []
3+
4+
public init(){}
5+
6+
public func register(warrior: Warrior){
7+
warriors.append(warrior)
8+
}
9+
10+
public func publish(_ msg: String, sender: Any){
11+
for w in warriors {
12+
if w !== sender as? Warrior {
13+
w.receive(msg)
14+
}
15+
}
16+
}
17+
18+
public func launchAttack(sender: Any) {
19+
for w in warriors {
20+
if w !== sender as? Warrior {
21+
w.engage()
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)