<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hrant Arzumanyan  on Medium]]></title>
        <description><![CDATA[Stories by Hrant Arzumanyan  on Medium]]></description>
        <link>https://medium.com/@4rzumanyan?source=rss-7b917404ab08------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BmskNVzFq-57uszubzhuqg.jpeg</url>
            <title>Stories by Hrant Arzumanyan  on Medium</title>
            <link>https://medium.com/@4rzumanyan?source=rss-7b917404ab08------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 03:08:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@4rzumanyan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Clean Architecture for Android and Not Only]]></title>
            <link>https://medium.com/@4rzumanyan/clean-architecture-for-android-and-not-only-016ca91a9339?source=rss-7b917404ab08------2</link>
            <guid isPermaLink="false">https://medium.com/p/016ca91a9339</guid>
            <dc:creator><![CDATA[Hrant Arzumanyan ]]></dc:creator>
            <pubDate>Thu, 15 Aug 2024 15:16:26 GMT</pubDate>
            <atom:updated>2024-08-15T15:16:26.006Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Table of Contents</strong></h3><p>· <a href="#3cdf">Introduction</a><br>· <a href="#de5e">Should You Use Clean Architecture?</a><br>· <a href="#d2b6">Understanding the Differences Between Architecture Patterns and Software Architectures</a><br>· <a href="#4d45">The Origins of Clean Architecture: Screaming Architecture</a><br>· <a href="#f62c">Uncle Bob’s Clean Architecture Principles</a><br>· <a href="#0cc4">Misconceptions Related to Clean Architecture</a><br>· <a href="#1710">From Uncle Bob to Fernando Cejas: Evolving Clean Architecture for Android</a><br>· <a href="#962b">The Dependency Rule</a><br>· <a href="#deac">Simplifying the Layers</a><br>· <a href="#5868">Mapping Data Between Layers</a><br>· <a href="#7033">Understanding Entities</a><br>· <a href="#454a">Clarifying Use Case and Interactor in Clean Architecture</a><br>· <a href="#7147">Conclusion</a></p><h3><strong>Introduction</strong></h3><p>At first glance, Clean Architecture seems like a simple<strong> </strong>set of recommendations for development. However, many beginners and even experienced developers don’t grasp the core concepts right away. In this article, I want to share my opinion on Clean Architecture and would be happy to hear your thoughts too.</p><h3><strong>Shell You Use Clean Architecture?</strong></h3><p>It’s important to note that it is up to each individual to decide whether to use these recommendations in their project. Even Uncle Bob, <a href="https://en.wikipedia.org/wiki/Robert_C._Martin">the author of the Clean Architecture idea</a>, mentioned this in <a href="https://groups.google.com/g/clean-code-discussion/c/mvP_NR2MUPc/m/wrkHHxzkDQAJ">one of his discussions</a>:</p><blockquote><strong>“And remember, we are all pirates by nature; and the rules I’m talking about here are really more like guidelines…”</strong></blockquote><blockquote><strong>Uncle Bob</strong></blockquote><p>If you are categorically against these recommendations and don’t want to hear other perspectives, you can stop reading the article at this moment (<em>No hard feelings — just don’t blame us if your code starts to resemble a spaghetti dinner!</em> 😊).</p><h3>Understanding the Differences Between Architecture Patterns and Software Architectures</h3><p>If you’re still with me, it means either you want to know other perspectives or you just don’t like spaghetti 😄.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WM05qaQssCW0Zx0915aZpQ.png" /></figure><p>As we are going to discuss Clean Architecture, especially for Android, I would like to address a common misconception related to <strong>Architecture Patterns</strong> and <strong>Software Architectures</strong>.</p><p><strong>Architecture Patterns</strong> such as <strong>MVVM</strong>, <strong>MVI</strong>, and <strong>MVP</strong> provide reusable solutions to common architectural challenges within specific contexts or domains of application development. These patterns guide the organization and interaction of code components but do not constitute complete software architectures in themselves.</p><p><strong>Software Architecture</strong>, on the other hand, refers to the overarching structure of a software system. It involves defining software elements, their relationships, and the properties that govern them. Software architecture encompasses the entirety of a system’s design and serves as the foundation upon which architecture patterns can be applied.</p><p>In summary, <strong>while architecture patterns address specific aspects of design within particular contexts, software architecture defines the overall structure and framework of a software system.</strong> Understanding these differences is crucial for effectively applying Clean Architecture principles.</p><h3><strong>The Origins of Clean Architecture: Screaming Architecture</strong></h3><p>Before diving into Clean Architecture, it’s crucial to discuss its origins.</p><p>In 2011, Uncle Bob published <a href="https://blog.cleancoder.com/uncle-bob/2011/09/30/Screaming-Architecture.html">an article</a> called <strong>“Screaming Architecture”</strong>, which argues that <strong>architecture should “scream” about the application itself, not what frameworks it uses.</strong> He provided an example: envisioning the architecture of a library might include a grand entrance, areas for clerks, reading spaces, conference rooms, and numerous galleries for bookshelves— elements that clearly embody the library’s purpose.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VAMQAS0tYFVWvBBAYwMGUg.jpeg" /><figcaption><strong>Architecture Library Floor Plan</strong></figcaption></figure><p>When describing a library, we focus on its features such as the grand entrance, reading spaces, and conference rooms rather than on general elements like the floor or roof. It means that architecture is about intent, and what the application does. He thinks we focus too much on details and frameworks and have made them the center of our systems.</p><p>Bringing a similar example to our environment, organizing projects around features rather than generic layers like Data, Domain, or Presentation prevents confusion when someone first encounters the project structure. That’s why I think adopting a <strong>Feature-Based</strong> approach is preferable to a <strong>Layer-Based</strong> one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*YBdkDeKdZjG9TkvB7FcNoQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*1llSoEkwPuYhGQVk7Klvpw.png" /><figcaption><strong>‏‏ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎ Layer-Based vs Feature-Based</strong></figcaption></figure><p>It organizes code by functionality rather than by technical layers, which aligns better with how users interact with the application. This structure makes it easier to locate and modify features, enhancing maintainability and scalability.</p><p>Additionally, it fosters a more modular design, reducing dependencies between different parts of the application. By grouping related components together, feature-based architecture also improves collaboration among development teams, allowing for more efficient and parallel development efforts.</p><h3><strong>Uncle Bob’s Clean Architecture Principles</strong></h3><p>Later, Uncle Bob published an <a href="https://blog.cleancoder.com/uncle-bob/2011/11/22/Clean-Architecture.html">article</a> rebuffing those who spoke out against the ideas of pure architecture. In 2012, he published “<a href="https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html"><strong>The Clean Architecture</strong></a>” which is the main description of this approach.</p><p>Uncle Bob highlighted that over the years, various architectural ideas have emerged, each aiming to address the same fundamental goal: the separation of concerns. These ideas include <strong>Hexagonal Architecture, Onion Architecture, Screaming Architecture, DCI, </strong>and<strong> BCE.</strong> Despite their differences, they share common objectives and methodologies.</p><p>All these architectures strive to divide software into layers, with a focus on maintaining at least one layer for business rules and another for interfaces. This separation is designed to achieve several key benefits:</p><ul><li><strong>Independence from Frameworks:</strong> The architecture should not rely on specific libraries or frameworks, allowing developers to use them as tools rather than constraints.</li><li><strong>Testability:</strong> Business rules can be tested independently of the UI, database, web server, or any other external components.</li><li><strong>UI Independence:</strong> The user interface can be modified or replaced without affecting the core business logic. For instance, a web UI could be swapped for a console UI without altering the underlying business rules.</li><li><strong>Database Independence:</strong> The architecture allows for easy replacement of one database system with another (e.g., Oracle to MongoDB) without impacting the business rules.</li><li><strong>Independence from External Agencies:</strong> The business rules operate in isolation from the external world, ensuring they remain unaffected by outside changes.</li></ul><p>Uncle Bob’s Clean Architecture emphasizes these principles to create robust, flexible, and maintainable software systems.</p><h3><strong>Misconceptions Related to Clean Architecture</strong></h3><p>In his article, Uncle Bob also provided a schema describing his idea. But before diving into that, let’s examine <strong>Google’s provided diagram of “Best Practice of Architecture”</strong> which is the author of many misconceptions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RKhcD6DzByOKMfra" /><figcaption><strong>Diagram of a typical app architecture (Google)</strong></figcaption></figure><p>Google suggests having at least two layers and considers the domain layer as an additional layer. I believe the best practice is to implement a full project architecture from the start because we can’t predict how large our project will grow (<em>in my opinion, this should not be considered over-engineering, even for a test task</em>).</p><p>Another point to note is the definition below the diagram, which states that arrows represent <strong>dependencies between classes</strong>. This definition is somewhat ambiguous and might contribute to the misconceptions.</p><p>Now, let’s compare it with the original schema of Clean Architecture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FNJ_3ViwJJzmAU40_FGmMQ.png" /><figcaption><strong>The original schema of Clean Architecture</strong></figcaption></figure><p>Instead of the expected Data, Domain, and Presentation layers, we see terms like UI, DB, Use Cases, Entities, and other familiar words. There are also many unfamiliar terms because this schema is not tailored to a specific environment.</p><p>Notice that the UI layer <strong>doesn’t directly interact</strong> with the Data layer. This indicates that we finally start to explore what Clean Architecture itself is.</p><p><em>Buckle up, we’re just getting to the juicy part!</em></p><h3><strong>From Uncle Bob to Fernando Cejas: Evolving Clean Architecture for Android</strong></h3><p>After two years of publishing Uncle Bob’s Clean Architecture, Fernando Cejas introduced his revolutionary take on Clean Architecture specifically for Android development. In his influential <a href="https://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/">article</a>, Fernando presents the following layer schema:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/0*8peDc2gc0aEKGBwL.png" /><figcaption><strong>The version of Fernando&#39;s schema</strong></figcaption></figure><p>Finally, we see the familiar layers: Data, Domain, and Presentation. But what are <strong>Interactors</strong> and <strong>Boundaries</strong> doing here? If we look closely, we’ll find these concepts in the original Clean Architecture schema:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cuCOYgwsQyaCDObj2zYDFQ.png" /><figcaption><strong>Flow of control from Controller to Presenter</strong></figcaption></figure><p>Let’s start by exploring Boundaries.</p><p>Layer transitions occur through Boundaries, which involve two interfaces: one for the query and one for the response (Input/OutputPort). Both of these interfaces belong to the inner layer (note their color in the picture).</p><p>Here’s how it works: The Controller calls the InputPort method, the UseCase implements it, and then the UseCase responds via the OutputPort interface, which is implemented by the Presenter. This way, data crosses the boundary between layers, but <strong>all dependencies point inward to the UseCase layer.</strong></p><h3><strong>The Dependency Rule</strong></h3><p>To understand why dependencies work the way they do in Clean Architecture, it’s essential to grasp the <strong>Dependency Inversion Principle</strong>, the final principle of <strong>SOLID</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I9H3yP03IP8DxR37pQH7Bw.jpeg" /><figcaption><strong>The Principles of SOLID</strong></figcaption></figure><p>This principle asserts that <strong>high-level modules should not depend on low-level modules; both should depend on abstractions.</strong> This promotes the decoupling of components through abstractions and interfaces, fostering flexibility and maintainability.</p><p><strong>The Dependency Inversion Principle is key to creating source code dependencies that oppose the traditional flow of control.</strong> We structure our code so that the direction of control flow doesn’t dictate the direction of dependencies. For instance, in Java, this is achieved by arranging interfaces and inheritance relationships to ensure that high-level modules depend on abstractions rather than concrete implementations.</p><p>In Clean Architecture, this principle is applied consistently across all boundaries. Dynamic polymorphism is leveraged to manage source code dependencies, ensuring they defy the flow of control. This approach allows us to adhere to the Dependency Rule regardless of control flow direction. By using abstractions and interfaces, we create a robust structure where dependencies are managed effectively, resulting in a more modular and adaptable system.</p><h3><strong>Simplifying the Layers</strong></h3><p>Also in the article, Uncle Bob mentions that the number of layers in an architecture is flexible; you can have any number, as long as the Dependency Rule is consistently applied.</p><p>When examining the diagram from Fernando Cejas’s article, it might initially seem like the author took this flexibility to heart and reduced the number of layers to just three. However, this simplification is more nuanced than it appears. Let’s explore how Fernando arrived at his version of the schema.</p><p>I found an interesting transformation of the original schema into Fernando’s schema on the Internet. The process starts by removing unnecessary elements from the original schema and <strong>renaming Gateways to Repositories</strong>, a term that is more familiar to many developers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/0*kaGWrUFvwkkvn2K_.png" /><figcaption><strong>Original schema without unnecessary elements for Android</strong></figcaption></figure><p>This transformation makes the architecture a bit clearer. Next, he cuts the layers into pieces and converts the schema into a block diagram, with colors still indicating layer associations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6kQP5amz0rRxbkIa.png" /><figcaption><strong>The schema converted into a block diagram</strong></figcaption></figure><p>According to Fernando, the colors denote the layers, and the arrow at the bottom represents the Dependency Rule.</p><p>With this block diagram, it’s easier to visualize the flow of data from the UI to the database or server and back. To enhance clarity further, Fernando categorizes the layers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/0*ggw7pJQbjaBycJ16.png" /></figure><p>Finally, the author compares the original and simplified schemas side by side:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zvFFMtlVw2pEtLOg.png" /></figure><p>He also provides a schema that is most understandable for visualizing the data flow (<em>red arrows show the flow of data</em>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1013/0*MUGjB6n2EMtVr5LY.png" /></figure><p>While the diagrams look wonderful and might make you think we’re done, there are still several things to cover before we can proudly declare this the longest article on Clean Architecture ever written! 🥹</p><h3><strong>Mapping Data Between Layers</strong></h3><p>Some programmers prefer to use different models for every layer, while others avoid maintaining numerous models. There is no absolute right or wrong approach here; it depends on your specific case. The advantage of mapping your data between layers is that changing the data in one layer does not affect another layer. However, mapping one model to a similar one can lead to a large number of duplicates, which violates the <strong>DRY (Don’t Repeat Yourself)</strong> principle.</p><p>When doing this mapping, it’s crucial to remember that data passed across a boundary <strong>should always be in the form that is most convenient for the inner circle</strong>. This means that the mapping should be done in the Interface Adapters layer (<em>green circle</em>).</p><h3><strong>Understanding Entities</strong></h3><p>The next topic I want to discuss is Entities. Many developers imagine these as <strong>POJO (Plain Old Java Object)</strong> classes that we work with in the data layer. In practice, we often use the word <strong>“entity”</strong> as a suffix for objects stored in databases (<em>e.g., OrderEntity</em>). This leads some developers to believe that Uncle Bob’s schema aligns with Google’s diagram, interpreting the yellow circle in his diagram as equivalent to their data layer.</p><p>However, this interpretation is incorrect. By reading Uncle Bob’s article or watching his <a href="https://youtu.be/Nsjsiz2A9mg?si=MAy5I9CRUy8z913w">presentation</a>, it becomes clear that his <strong>Entities encapsulate enterprise-wide business rules</strong>. An entity can be an object with methods or a set of data structures and functions. The form doesn’t matter as long as the entities can be used by multiple applications within the enterprise.</p><p>For example, in an online shopping application, we can consider PurchaseAgreement as an Entity.</p><p>So, the Entities layer contains:</p><ul><li><strong>Entities</strong>: Functions or objects with methods that implement business logic common to many applications (<em>or, if there is no business logic, the highest level logic of the application</em>).</li><li><strong>DTOs</strong>: Required to work and transition between layers.</li></ul><h3>Clarifying Use Case and Interactor in Clean Architecture</h3><p>I think we’ve reached the most understandable part of Clean Architecture. But even here, where we might assume there would be no misconceptions, they do exist. Let’s start by examining how Uncle Bob describes Use Cases. He states that <strong>they contain application-specific business rules</strong>. I think you already know this (<em>and if not, you anyway like spaghetti </em>😅).</p><p>And what about Interactor, you may ask. The whole problem lies in this because many people think that an interactor is simply a set of Use Cases. If we look at the article where Uncle Bob defines the meaning of an Interactor, we find this:</p><blockquote><strong>“…interactor object that implements the use case by invoking business objects.”</strong></blockquote><p>This is essentially the same as what we usually call UseCaseImpl in our projects.</p><p>Although many people are accustomed to combining interactors into one common entity, it’s incorrect to define it as a set of Use Cases. If you don’t like having multiple classes, you can choose to consolidate them — it’s your decision. Personally, I advocate for separate Interactors, as it provides more flexibility. After all, flexibility is the cornerstone of Clean Architecture, allowing us to adapt and evolve our codebase without sacrificing integrity.</p><h3>Conclusion</h3><p>We’ve journeyed through the depths of Clean Architecture, exploring its origins, principles, and nuances, especially as they pertain to Android development. From Uncle Bob’s initial vision to Fernando Cejas’s Android-specific adaptation, we’ve dissected the layers, clarified common misconceptions, and demystified key concepts such as Use Cases, Interactors, and Entities.</p><p>To recap, <strong>Clean Architecture is all about maintaining a separation of concerns, ensuring that the core business logic remains independent of frameworks, user interfaces, and external databases.</strong> This independence fosters a more testable, flexible, and maintainable system. By adhering to a principle like the Dependency Inversion Principle, we create a robust structure where changes in one part of the system don’t ripple through and cause chaos elsewhere.</p><p>We also delved into the practical aspects of implementing Clean Architecture, such as mapping data between layers, understanding the difference between architecture patterns and software architectures, and the crucial role of boundaries in facilitating communication between layers.</p><p>Here’s a schema I drew several years ago, highlighting what I believe to be the best in architectural practices. It synthesizes all we’ve discussed and offers a visual representation to aid your understanding and implementation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UXTrgUiE4qbNr3Vb41ZENA.png" /></figure><p>As you continue your development journey, remember that Clean Architecture is not a one-size-fits-all solution but a set of guidelines designed to help you build scalable and maintainable applications. Whether you’re working on a small project or a large enterprise application, the principles of Clean Architecture can be adapted to meet your needs.</p><p>Thank you for sticking with me through this comprehensive exploration. If you have any thoughts, questions, or additional insights, feel free to share. Happy coding, and may your architecture always be clean and your code free of spaghetti!</p><h3>Links and Resources</h3><ul><li><a href="https://developer.android.com/topic/architecture">Guide to app architecture (Google)</a></li><li><a href="https://blog.cleancoder.com/uncle-bob/2011/09/30/Screaming-Architecture.html">Screaming Architecture (Uncle Bob)</a></li><li><a href="https://blog.cleancoder.com/uncle-bob/2011/11/22/Clean-Architecture.html">Clean Architecture: The First Publishing (Uncle Bob)</a></li><li><a href="https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html">The Clean Architecture (Uncle Bob)</a></li><li><a href="https://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/">Architecting Android… The clean way? (Fernando Cejas)</a></li><li><a href="https://teletype.in/@skillbranch/BkDt3KHMU">Misconceptions of Clean Architecture (SkillBranch)</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=016ca91a9339" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>