Skip to content

Commit

Permalink
definitions for all of solid principles, more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hlazarpesic committed Jan 18, 2020
1 parent 9617f3d commit b9ca64b
Show file tree
Hide file tree
Showing 26 changed files with 179 additions and 8 deletions.
Binary file modified Design Patterns in Swift/.DS_Store
Binary file not shown.
Binary file added Design Patterns in Swift/Solid/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Foundation

//The Dependency Inversion Principle states that:
///1. High-level modules should not depend on low-level modules. Both should depend on abstractions.
///2. Abstractions should not depend upon details. Details should depend upon abstractions.

protocol IWorker {

func work()
}

class Worker: IWorker { //low-level

func work() {

}
}

class SuperWorker: IWorker {

func work() {

}
}

class Manager { //high-level

var worker: IWorker!

func setWoker(_ worker: IWorker) {
self.worker = worker
}

func manage() {
worker.work()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
<playground version='6.0' target-platform='ios'>
<pages>
<page name='Worker and manager'/>
<page name='Relatioship example'/>
</pages>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import UIKit
import Foundation

//No client should be forced to depend on methods it does not use.

class Document {

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import UIKit
import Foundation

//Child classes should never break the parent class' type definitions.
///Example where is substitution principle is broken

class Rectangle: CustomStringConvertible {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import UIKit
import Foundation

enum Color {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Foundation

//Software entities (classes, modules, functions, etc) should be open for extension, but closed for modification.

protocol Shape {

func getArea() -> Double
}

class Rectangle: Shape {

var length: Double
var height: Double

init(_ length: Double, _ height: Double) {
self.length = length
self.height = height
}

func getArea() -> Double {
return length * height
}
}

class Circle {

var radius: Double

init(_ radius: Double) {
self.radius = radius
}

func getArea() -> Double {
return radius * radius * 3.14
}
}

class AreaManager {

func calculateArea(shapes: [Shape]) -> Double {
var area: Double = 0.0

for shape in shapes {
area += shape.getArea()
}

return area
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
<playground version='6.0' target-platform='ios'>
<pages>
<page name='Shapes'/>
<page name='Filtering and Specification'/>
</pages>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import UIKit

//A class should have one, and only one, reason to change.

class Journal: CustomStringConvertible {

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.

0 comments on commit b9ca64b

Please sign in to comment.