Reviewed by @Anonymous @Anonymous @Anonymous
Notes

What is SOLID?

SOLID is a set of five object-oriented design principles that can help you write more maintainable, flexible, and scalable code based on well-designed, cleanly structured classes. These principles are a fundamental part of object-oriented design best practices.

Principles usage

You can see here a example of usage of the principles with some DO’s and DONT’s.

1_jPROahDWDQ2fWnKixUZhAw.png

Single responsibility

Class has one job to do. Each change in requirements can be done by changing just one class.

DON’T

// Single Responsibility Principle Violation
class SystemManager { 
	fun addUser(user: User) {...} 
	fun deleteUser(user: User) {...} 
	fun sendNotification(notification:String) {...}
}

DO

// Single Responsibility Principle Correct Usage
class NotificationManager() { 
	fun sendNotification(notification: String) {...}
}
class UserManager { 
	fun addUser(user: User) {...} 
	fun deleteUser(user: User) {...}
}

Open/closed principle

Class is happy (open) to be used by others. Class is not happy (closed) to be changed by others.

DON’T

// Open/Closed Principle Violation
class Shape(val type: String, val width: Double, val height: Double)
fun calculateArea(shape: Shape): Double { 
	if (shape.type == "rectangle") { 
		return shape.width * shape.height } 
	else if (shape.type == "circle") { 
		return Math.PI * shape.width * shape.width 
	} 
	return 0.0
}

DO

// Open/Closed Principle Correct Usage
interface Shape { 
	fun area(): Double
}

class Rectangle(val width: Double, val height: Double) : Shape { 
	override fun area() = width * height
}

class Circle(val radius: Double) : Shape { 
	override fun area() = Math.PI * radius * radius
}

fun calculateArea(shape: Shape) = shape.area()