<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yasmmin Claudino</title>
    <description>The latest articles on DEV Community by Yasmmin Claudino (@yasmmin).</description>
    <link>https://dev.to/yasmmin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1424784%2Fbcdab439-b62f-462d-9741-015af4e46b66.jpeg</url>
      <title>DEV Community: Yasmmin Claudino</title>
      <link>https://dev.to/yasmmin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yasmmin"/>
    <language>en</language>
    <item>
      <title>Why use a well structured architecture in Android?</title>
      <dc:creator>Yasmmin Claudino</dc:creator>
      <pubDate>Mon, 02 Sep 2024 22:40:23 +0000</pubDate>
      <link>https://dev.to/yasmmin/why-use-a-well-structured-architecture-in-android-28j</link>
      <guid>https://dev.to/yasmmin/why-use-a-well-structured-architecture-in-android-28j</guid>
      <description>&lt;p&gt;The Android Operating System (AOSP) is designed to enhance the user experience by managing system resources efficiently. To maintain optimal performance, it may terminate processes as needed. If it "decides" to kill your process, there's little you can do to prevent it. Therefore, maintaining a consistent architecture is crucial to avoid data loss. Understanding the principle of separation of concerns is essential as you embark on your journey with Android architecture.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separation of Concerns
&lt;/h2&gt;

&lt;p&gt;Separation of Concerns (SoC) is a principle that organizes code into distinct layers, each representing a specific aspect of the application, such as the data layer, UI layer, and business logic layer. This structure enhances code maintainability, readability, and comprehension, facilitating a quicker learning curve for newcomers. SoC also promotes code reusability and simplifies updates by allowing changes to be made within individual layers without impacting others.&lt;/p&gt;

&lt;p&gt;In the book &lt;em&gt;Elements of Functional Programming&lt;/em&gt;, the concept of separation of concerns is explored through several key topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Describing what is to be computed;&lt;/li&gt;
&lt;li&gt;Organizing the computation sequencing into small steps;&lt;/li&gt;
&lt;li&gt;Managing memory during computation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  UI Layer
&lt;/h2&gt;

&lt;p&gt;The UI layer encompasses all aspects of the user interface and serves as the entry point of the application. It contains everything the user needs to see and interact with. However, the data displayed in the UI layer is typically loaded from another distinct layer known as the Data Layer.&lt;/p&gt;

&lt;p&gt;The UI state should not be updated directly within the UI components. Instead, it should utilize a state holder. One of the most well-known state holders on Android is the &lt;code&gt;ViewModel&lt;/code&gt;. The &lt;code&gt;ViewModel&lt;/code&gt; classes define the logic applied to events in the app and produce updated states as a result.&lt;/p&gt;

&lt;p&gt;However, &lt;code&gt;ViewModel&lt;/code&gt;s, or view holders, have a lifecycle that extends beyond that of an &lt;code&gt;Activity&lt;/code&gt;. They should not manage any UI flow directly to avoid memory leaks. Additionally, observables and flow collections should only be activated to observe or collect data when the activity starts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layer Communication
&lt;/h2&gt;

&lt;p&gt;As illustrated in the image below, the &lt;code&gt;ViewModel&lt;/code&gt; is an integral part of the UI Layer, responsible for responding to UI element interactions initiated by the user. For instance, when a user clicks a button, this triggers an event that is relayed to the &lt;code&gt;ViewModel&lt;/code&gt;. The &lt;code&gt;ViewModel&lt;/code&gt; then processes the necessary logic and forwards the request to the Data Layer. Upon receiving a response, the &lt;code&gt;ViewModel&lt;/code&gt; updates the UI with the new state. The updates in data are either observed or collected—using &lt;code&gt;LiveData&lt;/code&gt; for observation or &lt;code&gt;StateFlow&lt;/code&gt; for collection.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1hmb2azvuapvyy1mii2i.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1hmb2azvuapvyy1mii2i.jpeg" alt="Image description" width="800" height="1034"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Layer
&lt;/h2&gt;

&lt;p&gt;The Data Layer is responsible for managing all application data and business logic. This separation centralizes information management away from the UI Layer and facilitates unit testing. It specifies the protocols for data creation, storage, and modification.&lt;/p&gt;

&lt;p&gt;The Data Layer consists of repositories—classes responsible for handling specific types of data. Each repository is responsible for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exclusively exposing data to the application;&lt;/li&gt;
&lt;li&gt;Modifying the data;&lt;/li&gt;
&lt;li&gt;Resolving conflicts between data sources;&lt;/li&gt;
&lt;li&gt;Incorporating business logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Now let's see a bit of how this would look in real life:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here’s an example of how layer communication works. The &lt;code&gt;Activity&lt;/code&gt; should not directly manipulate the data. Instead, it should use the &lt;code&gt;ViewModel&lt;/code&gt; to access data from the repository. The &lt;code&gt;ViewModel&lt;/code&gt; gets an instance of the repository through dependency injection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookListViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;BookRepository&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;_books&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableLiveData&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BookResponse&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;books&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LiveData&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BookResponse&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_books&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;searchBooks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="n"&gt;_books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getBooks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding and implementing the principle of separation of concerns is a crucial step in mastering Android development. By clearly defining and separating the responsibilities of different layers in your application, you can create code that is easier to maintain, test, and scale.&lt;/p&gt;

&lt;p&gt;Feel free to share your thoughts or questions in the comments.&lt;br&gt;
See you in the next topic!&lt;/p&gt;

</description>
      <category>android</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
