Skip to content

Commit d01b9a6

Browse files
author
Sebastian Boldt
committed
added example for open close principle
1 parent 6ff3f75 commit d01b9a6

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed
Binary file not shown.

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# GoF & S.O.L.I.D in Swift
1+
# GoF & SOLID in Swift
22
👨‍👩‍👧‍👦 My personal Repo to learn all GOF und SOLID Patterns using Swift and Playgrounds
33

44
✅ Implemented and internalized
@@ -59,13 +59,13 @@
5959

6060
✅ Virtual Proxy
6161

62-
## S.O.L.I.D
62+
## SOLID
6363

6464
✅ Dependency Inversion Principle (DIP)
6565

6666
✅ Single Responsibilty Principle (SRP)
6767

68-
Open Closed Principle (OCP)
68+
Open Closed Principle (OCP)
6969

7070
❌ Liskov Substitution Principle (LSP)
7171

SOLID.playground/Pages/Open Closes Principle.xcplaygroundpage/Contents.swift

+31
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,35 @@ import Foundation
44

55
// SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.) SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR MODIFICATION.
66

7+
protocol Printable {
8+
func printElement()
9+
}
10+
11+
public struct Printer {
12+
func print(printables: [Printable]) {
13+
for printable in printables {
14+
printable.printElement()
15+
}
16+
}
17+
}
18+
19+
struct Student: Printable {
20+
var name: String
21+
22+
func printElement() {
23+
print("student \(name)")
24+
}
25+
}
26+
27+
struct Teacher: Printable {
28+
var name: String
29+
func printElement() {
30+
print("teacher: \(name)")
31+
}
32+
}
33+
34+
35+
let printables: [Printable] = [Student(name: "Peter"), Teacher(name: "James")]
36+
Printer().print(printables: printables)
37+
738
//: [Next](@next)

0 commit comments

Comments
 (0)