}
class Square: Shape {
var side: Double
init(side: Double) {
self.side = side
}
var area: Double {
return side * side
}
}
```
### 4. Interface Segregation Principle (ISP):
4. **Interface Segregation Principle (ISP):** A class should not be forced to implement interfaces it does not
use. Clients should not be forced to depend on interfaces they do not use.
Example: Breaking Down a Monolithic Protocol
```swift
// Not following ISP
protocol Worker {
func work()
func takeBreak()
func attendMeeting()
}
// Following ISP
protocol Workable {
func work()
}
protocol Breakable {
func takeBreak()
}
protocol Attendable {
func attendMeeting()
}
class Employee: Workable, Breakable, Attendable {
// Implement the specific protocols
}
```
### 5. Dependency Inversion Principle (DIP):
5. **Dependency Inversion Principle (DIP):** High-level modules should not depend on low-level modules;
both should depend on abstractions. Abstractions should not depend on details; details should depend on
abstractions.
Example: Inversion of Control with Dependency Injection
```swift
// Not following DIP
class Service {
// Service has a dependency on the concrete implementation
private let database: Database = Database()
func fetchData() {
// Use the database to fetch data