A good software architecture is essential for a product’s success. A poorly designed architecture can lead to spaghetti code, high coupling, low cohesion, technical debt, and a decrease in productivty. Software architecture principles such as loose coupling and high cohesion are important in designing clean architecture. In this blog, we’ll explore what a Clean Architecture is, its benefits, and how loose coupling and high cohesion fit into it with code examples.
What is The Clean Architecture?
The Clean Architecture is an approach to software development that promotes a separation of concerns and the modularization of a system’s components. It’s a combination of concepts from various software design patterns, including Hexagonal Architecture, Onion Architecture, and Dependency Inversion Principle.
The Clean Architecture divides the system into different layers, each with its specific purpose and responsibilities. The main goal of The Clean Architecture is to keep the domain logic independent of external concerns and infrastructure. It makes the codebase easy to maintain, test, and extend.
The Benefits of The Clean Architecture
The advantages of The Clean Architecture are numerous. Here are some of them:
-
Maintainability: The Clean Architecture promotes the separation of concerns and modularization, making it easier to maintain the system over the long run.
-
Testability: The Clean Architecture assists in creating more testable code. It promotes modular design, so each component is easier to test in isolation.
-
Scalability: The Clean Architecture encourages loose coupling and high cohesion, making it easy to modify and extend the system’s functionality.
-
Flexibility: The Clean Architecture’s design patterns make it easy to modify and maintain the system without disturbing other parts of the code.
How Loose Coupling and High Cohesion Fit into The Clean Architecture
Loose Coupling and High Cohesion are central principles in The Clean Architecture. Here’s how they work:
- Loose Coupling: Loose Coupling refers to the ability of software components to maintain independence from one another. We strive to reduce interdependencies between modules or components. The goal is to design each module to be independent and able to function on its own. Here’s an example:
protocol DataAccess {
func fetchData() -> Data
}
class DataAccessImpl: DataAccess {
func fetchData() -> Data {
return Data()
}
}
protocol ViewModelProtocol {
func request()
}
class ViewModel: ViewModelProtocol {
let dataAccess: DataAccess
init(dataAccess: DataAccess) {
self.dataAccess = dataAccess
}
func request() {
let data = dataAccess.fetchData()
// use the data
}
}
In the example above, the ViewModel
object is dependent on the DataAccessImpl
object for data, but it’s not tightly bound to it. The two objects are decoupled, and either one of them can be replaced without affecting the other.
- High Cohesion: High Cohesion refers to the measure of how closely the elements of a module relate to each other. We strive to create components with a single purpose or responsibility. In The Clean Architecture, we create modules that are cohesive and contain functions or responsibilities that are related to each other. Here’s an example:
protocol ModelProtocol {
func fetchItems() -> [String]
}
class Model: ModelProtocol {
func fetchItems() -> [String] {
// fetch the items
return []
}
}
protocol ViewProtocol {
func displayItems(_ items: [String])
}
class View: ViewProtocol {
func displayItems(_ items: [String]) {
// display the items
}
}
class Presenter {
let model: ModelProtocol
let view: ViewProtocol
init(model: ModelProtocol, view: ViewProtocol) {
self.model = model
self.view = view
}
func didTapFetchItems() {
let items = model.fetchItems()
view.displayItems(items)
}
}
In the example above, each component has a single responsibility. The Model
fetches the data, the View
displays the items, and the Presenter
acts as the mediator between them. The components are highly cohesive, and any modifications to one module do not affect the others.
Conclusion
The Clean Architecture is a powerful approach to software development that produces clean, testable, maintainable, and scalable code. It promotes a separation of concerns and modularization of the system into distinct layers. Loose Coupling and High Cohesion are essential principles in building a clean architecture, ensuring that the modules or components are independent, cohesive, and functionally separate without being tightly bound to each other. By following the principles of The Clean Architecture, developers can create software that is easy to maintain and extend over the long run, ultimately resulting in a robust and reliable product.