Contact Us

V-Techhub: Apply Clean Architecture

Android, V-TechHub | June 18, 2024
Liberty - Mobile Developer

Liberty – Mobile Developer

Calling all coders – it’s Vinova’s monthly IT knowledge drop, and this time, our very own Mobile Developer, Liberty, is here to share some secrets of the trade.

Today, we will talk about Clean Architecture—a way to organize our code so that it’s easier to maintain, test, and expand. We’ll explore what Clean Architecture is, its benefits, and how we can apply it to our mobile projects.

What is Software Architecture?

When developing software, it is essential to answer several key questions:

  1. What problem is the software solving?
  2. What are the functional requirements?
  3. What are the non-functional requirements?
  4. How complex is the data flow?

Software architecture refers to the high-level structure of a software system, defining how different components interact and work together. It serves as a blueprint that guides the design and implementation of the system. Good architecture ensures the system is maintainable, scalable, and adaptable to changes.

Several software design approaches are available, including n-Tier, microservices, clean architecture, etc.

Software architecture is crucial for building robust, maintainable, and scalable systems. By understanding key concepts and selecting an appropriate architectural style, we can design software that meets user and stakeholder needs while remaining adaptable to future changes. Whether working on a small project or a large enterprise application, a solid architectural foundation is vital for success.

Key Ideas in Clean Architecture

When discussing software design, two critical concepts are dependency and domain.

Minimizing dependency between components within software reduces maintenance costs. Here are some key principles:

  • Avoid direct dependencies on concrete classes, as they often depend on many other components.
  • Avoid cyclic dependencies, where components depend on each other in a loop.
  • All software design and architecture methods emphasize reducing dependencies or isolating components from each other using interfaces or APIs. For example, Clean Architecture organizes software into layers, allowing dependencies in only one direction.

As Robert Harvey stated, “The domain is the real-world context in which you are attempting to solve a problem using software.” The domain layer contains our core business logic and includes entities that represent key business objects. In a shopping app, for instance, entities might include Product, Order, and Customer. This layer should be independent of other layers and encapsulate core business rules.

Analyze Clean Architecture

 Clean Architecture

This is a common chart of Clean Architecture’s framework, analyzing how communication and arrangement within a software system affect its maintainability, scalability, and cost.

Clean Architecture chart

To effectively manage complexity in software design, we divide the problem into core components. We begin by focusing on Entities, which represent the core elements of our real-world problem domain. These entities are technology-independent – whether you manage them on paper, on a computer, or in the cloud, they remain the fundamental building blocks.

The key to effective entity management lies in ensuring their independence from other components. Here’s where the concept of Interfaces comes in.  They act as an abstraction layer, separating the core domain logic (entities) from the concrete implementation details (technology-specific solutions).  

This technique, known as dependency inversion, ensures that entities remain independent and adaptable to future changes, fostering maintainable and flexible software.

 dependency inversion

We’ve established Entities as the core, independent components representing real-world concepts. To ensure their maintainability and flexibility, we need to minimize their dependence on other components. This is where Dependency Inversion comes into play.

The chart above depicts the importance of each layer from left to right so Entity – the important component is the lowest changeable. In other words, it’s independent and least likely to change.

Use-case defines functionalities Entities can perform, without implementation specifics. It performs dependently on Interface but the Use Case does not make up the problem that needs to be solved frequently, because It declares function and not a concrete class. Thus, it does not depend on any object outside but Use Case. 

The Implementation is the outermost layer is commonly services like network, database server, etc. Concrete classes implementing functionalities (leverages technologies like databases).

Crucially, we can see both Use Case and Interface depend on Entity (core data and logic), and Entity remains independent (doesn’t rely on outer layers). And respectively, Use Case depends on Interface (functionalities).

Conclusion

Clean Architecture is a modern approach to structuring our codebase that brings numerous benefits, including improved maintainability, testability, and scalability. By applying its principles to our mobile projects, you can build robust and flexible applications that stand the test of time. Embrace Clean Architecture and take our software development skills to the next level.

Happy coding!