0% found this document useful (0 votes)
6 views1 page

Solid OCP Swift

Uploaded by

Amazing Deals
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Solid OCP Swift

Uploaded by

Amazing Deals
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

let objEmp = Employee(httpHandler: HttpHandler(), parser: Parser(), databaseHandler: DatabaseHandler())

### 2. Open/Closed Principle (OCP):


2. **Open/Closed Principle (OCP):** Software entities (classes, modules, functions, etc.) should be open for
extension but closed for modification. You can add new features without changing existing code.

Example: Extensible Logging System

protocol Shape {
func calculateArea() -> Double
}

class Circle: Shape {

let radius: Double

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

func calculateArea() -> Double {


return Double.pi * radius * radius
}
}

class Triangle: Shape {

let base: Double


let height: Double

init(base: Double, height: Double) {


self.base = base
self.height = height
}

func calculateArea() -> Double {


return 1/2 * base * height
}

class Rectangle: Shape {

let width: Double


let height: Double

init(width: Double, height: Double) {


self.width = width
self.height = height
}

func calculateArea() -> Double {


return width * height
}
}

class AreaCalculator {
func area(shape: Shape) -> Double {
return shape.calculateArea()

You might also like