<?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 Amirul Nizam Alfian on Medium]]></title>
        <description><![CDATA[Stories by Amirul Nizam Alfian on Medium]]></description>
        <link>https://medium.com/@nizamalfian?source=rss-86648c9fbd3c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Zb8f4D7h3aq-rxF-nH7N-A@2x.jpeg</url>
            <title>Stories by Amirul Nizam Alfian on Medium</title>
            <link>https://medium.com/@nizamalfian?source=rss-86648c9fbd3c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 11:38:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nizamalfian/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[Handling Multiple View in RecyclerView Adapter Using Visitor Pattern on Android — Part 3/3]]></title>
            <link>https://medium.com/kurio-toolbox/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-3-3-b2c9d3a15f35?source=rss-86648c9fbd3c------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2c9d3a15f35</guid>
            <category><![CDATA[adapter-recyclerview]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[oop-concepts]]></category>
            <dc:creator><![CDATA[Amirul Nizam Alfian]]></dc:creator>
            <pubDate>Wed, 01 Jul 2020 05:07:33 GMT</pubDate>
            <atom:updated>2020-07-01T05:07:33.608Z</atom:updated>
            <content:encoded><![CDATA[<h3>Handling Multiple View in RecyclerView Adapter Using Visitor Pattern on Android — Part 3/3</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QJjp-D3MfIuhaqhE" /><figcaption>Photo by <a href="https://unsplash.com/@rocinante_11?utm_source=medium&amp;utm_medium=referral">Mick Haupt</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In <a href="https://toolbox.kurio.co.id/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-2-3-fd39125f4195"><strong>part II</strong></a>, I just explained how Visitor Pattern works with RecyclerView Adapter. Unfortunately, it is possible that the Visitor will have boilerplates especially in creating the right ViewHolder based on its layout resource.</p><p>Here’s the problem:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6d5142abfa21c9a8aeeb0020150cd7b/href">https://medium.com/media/e6d5142abfa21c9a8aeeb0020150cd7b/href</a></iframe><p>Once we declare layout resource inside <strong>visit() </strong>generated from an element with layout type validation, we must mention all pairs of layout resource and its ViewHolder. For the sake of giving a list of ViewHolders, that is quite an extra step even a ViewHolder must be created from the right layout resource. Quite clear it opposes a <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">D.R.Y</a> principle.</p><h3>Two layers of Double Dispatch</h3><p>In doing my own Visitor Pattern practice, I wondered why do we have a Visitor Pattern implemented in our Adapter but it had been recurred by another design structure in the Visitor.</p><p>After doing my research, I just found the solution to reduce the recurring design structure even it might not be the most right way. I made 1 more Visitor cover this issue. So I have 2 layers of Visitor:</p><ol><li><strong>ViewHolder Visitor</strong>:<strong> </strong>consists in taking what layout resource and ViewHolder would be generated, no role changed on this existing Visitor.</li><li><strong>ItemView Visitor</strong>: delivers the valid layout resource and ViewHolder to every single entity related to the FeedContent element and the layout resource validation, relates to the elements called ItemView.</li></ol><h3>Please welcome, a new Visitor</h3><p>Before we dive into the Visitor, let’s define what elements are needed by the ItemView Visitor.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f67159f926a596b2ffad7f8fb3cfb4ea/href">https://medium.com/media/f67159f926a596b2ffad7f8fb3cfb4ea/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3983669820010b5b7a8f90aa1c9d016/href">https://medium.com/media/d3983669820010b5b7a8f90aa1c9d016/href</a></iframe><p>One more Single Responsibility Principle created, <strong>LayoutFactory</strong> brings the way.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/983690a5922b9440efcb24ce980868e2/href">https://medium.com/media/983690a5922b9440efcb24ce980868e2/href</a></iframe><p>Instead of using the conditional validation, SparseArray makes it a bit more maintainable.</p><p>Lastly, the Visitor:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7da6acdc1c90408588bf42223051a5e0/href">https://medium.com/media/7da6acdc1c90408588bf42223051a5e0/href</a></iframe><p>One more thing, while the new Visitor was finished, the previous Visitor became the client of ItemViewVisitor.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63ca4526189a9ef40ea2c9fec47982e3/href">https://medium.com/media/63ca4526189a9ef40ea2c9fec47982e3/href</a></iframe><p>Of course, we have to maintain 2 visitors and 2 kinds of elements. But my point here is how we reduce too much complex nested if statements to limit the scope of each responsibility of an object.</p><blockquote><strong>PS</strong>: This enhancement might have a different implementation to another app depends on the business logic and requirement.</blockquote><p><strong>1. </strong><a href="https://toolbox.kurio.co.id/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-1-3-181b2989b22"><strong>Part I — The Fundamental and Simple Case</strong></a></p><p><strong>2. </strong><a href="https://toolbox.kurio.co.id/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-2-3-fd39125f4195"><strong>Part II — Implementation to RecyclerView Adapter</strong></a></p><p>3. Part III — The Enhancement of the Visitor (optional)</p><p>That’s all, let’s have fun with recycling!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2c9d3a15f35" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kurio-toolbox/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-3-3-b2c9d3a15f35">Handling Multiple View in RecyclerView Adapter Using Visitor Pattern on Android — Part 3/3</a> was originally published in <a href="https://medium.com/kurio-toolbox">Kurio Toolbox</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handling Multiple View in RecyclerView Adapter Using Visitor Pattern on Android — Part 2/3]]></title>
            <link>https://medium.com/kurio-toolbox/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-2-3-fd39125f4195?source=rss-86648c9fbd3c------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd39125f4195</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[adapter-recyclerview]]></category>
            <category><![CDATA[oop-concepts]]></category>
            <dc:creator><![CDATA[Amirul Nizam Alfian]]></dc:creator>
            <pubDate>Wed, 01 Jul 2020 05:07:21 GMT</pubDate>
            <atom:updated>2020-07-01T05:07:21.893Z</atom:updated>
            <content:encoded><![CDATA[<h3>Handling Multiple View in RecyclerView Adapter Using Visitor Pattern on Android — Part 2/3</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2akNE25IX9LiALSI" /><figcaption>Photo by <a href="https://unsplash.com/@siora18?utm_source=medium&amp;utm_medium=referral">Siora Photography</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Previously in <a href="https://toolbox.kurio.co.id/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-1-3-181b2989b22"><strong>part I</strong></a>, we discuss about the introduction of the Visitor Pattern and how it works on the simple case. So, in this part, I will explain how to do Visitor Pattern in RecyclerView Adapter case.</p><h3>Implementation to RecyclerView Adapter</h3><p>Double Dispatch is the fundamental of using Visitor Pattern. Whatever the implementation on the cases we have, I recommend you to understand Double Dispatch properly.</p><p>In this example, I adjust the case related to <a href="https://play.google.com/store/apps/details?id=com.merahputih.kurio">Kurio</a> app. As a news app, it provides multiple views content based on the item type either content type (article, canvas, podcast episode, etc…) and layout (thumbnail-text-left, big-thumbnail, thumbnail-bordered, etc…).</p><p>First thing first, we declare the Visitor structure. Don’t forget to provide all available elements would be rendered. It also adds layout as a String value to the parameter because in one content type has a more than one layout type. The validation would be based on the layout type. And the function of <strong>visit() </strong>is to return an Integer value to generate layout resources.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/721b73ab0d723fdfc7b6eed09ba601df/href">https://medium.com/media/721b73ab0d723fdfc7b6eed09ba601df/href</a></iframe><p>Furthermore, we have an element model such as an Article, Canvas, and Episode. These elements belong to FeedContent inside the FeedItem.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/656f2b837e821be44acefc759cc7d459/href">https://medium.com/media/656f2b837e821be44acefc759cc7d459/href</a></iframe><p>Every element, FeedContent on this case, must be referenced to its own visiting methods either using overloading function or naming every function based on what is an element used with. <strong>“this” </strong>refers to the specific concrete subclass of an element.</p><p>Then, we have a Visitor implementation class named <strong>ViewHolderLayoutVisitor</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6d5142abfa21c9a8aeeb0020150cd7b/href">https://medium.com/media/e6d5142abfa21c9a8aeeb0020150cd7b/href</a></iframe><h4>Hmm, wait… It seems different from a simple case before.</h4><p>Why do we need <strong>create()</strong><em> </em>function?</p><p>In order to create the instance of ViewHolder, we make <strong>create() </strong>to be called in the RecyclerView Adapter specifically in the override function name <strong>onCreateViewHolder()</strong>. What the ViewHolder would be defined is exactly based on the layout resource. Keep in mind that we already have all ViewHolders including the DefaultViewHolder to handle incompatible ViewHolder type.</p><h3>Here it comes, our RecyclerView Adapter using Visitor Pattern…</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f8ba60d4269257f704e5d07149bf6d1/href">https://medium.com/media/1f8ba60d4269257f704e5d07149bf6d1/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed1325a7708714dab3a168985fffa2e5/href">https://medium.com/media/ed1325a7708714dab3a168985fffa2e5/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*1FzN6vHGzt9fvGJI1RP3MQ.gif" /><figcaption>Demo app</figcaption></figure><p>This is nice! Look, if we have a new kind of FeedContent or new layout type to one of the available FeedContents, no need to maintain the Adapter which has implemented the Visitor. All we need to focus on are:</p><ol><li>New model — as an element member,</li><li>Visitor — just add a new method to provide the new element, and</li><li>New ViewHolder including its layout resource, exactly.</li></ol><h3>The Pros:</h3><ol><li>We have a <strong>Single Responsibility Principle</strong> — we can work with multiple view types of ViewHolder and multiple elements (FeedContent) on each class.</li><li>We have an <strong>Open/Closed Principle</strong> — we clearly can declare new behavior (such as a new FeedContent element) related to the structure (RecyclerView Adapter) and another object in different classes (existing FeedContent elements) without modifying it.</li><li>We have a <strong>Visitor</strong> — a “ViewHolders map” of our app.</li></ol><h3>The Cons:</h3><ol><li>Always keep an eye on the Visitor! Visitor!! Visitor!!! Especially if the ViewHolder or element has been added/updated/removed. But so far, I have no better idea to solve this.</li><li>Indeed, it doesn’t rule out the possibility to have a bunch of ViewHolders and elements. But imagine we have dozens of elements, we have hundred of ViewHolders, yes please, just imagine it. Even seems cleaner, I personally pretty sure it would be boilerplate code “v2.0”.</li><li>Hmm, hold on… I think the Visitor above tends to have dirty codes, it might be too much ViewHolder validations based on its layout resource.</li></ol><p>The last point of the cons will be solved in the next part.</p><p><strong>1. </strong><a href="https://toolbox.kurio.co.id/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-1-3-181b2989b22"><strong>Part I — The Fundamental and Simple Case</strong></a></p><p>2. Part II — Implementation to RecyclerView Adapter</p><p><strong>3. </strong><a href="https://toolbox.kurio.co.id/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-3-3-b2c9d3a15f35"><strong>Part III — The Enhancement of the Visitor (optional)</strong></a></p><p>Any further discussions? Please be kind to comment below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd39125f4195" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kurio-toolbox/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-2-3-fd39125f4195">Handling Multiple View in RecyclerView Adapter Using Visitor Pattern on Android — Part 2/3</a> was originally published in <a href="https://medium.com/kurio-toolbox">Kurio Toolbox</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handling Multiple View in RecyclerView Adapter Using Visitor Pattern on Android — Part 1/3]]></title>
            <link>https://medium.com/kurio-toolbox/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-1-3-181b2989b22?source=rss-86648c9fbd3c------2</link>
            <guid isPermaLink="false">https://medium.com/p/181b2989b22</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[adapter]]></category>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[oop-concepts]]></category>
            <dc:creator><![CDATA[Amirul Nizam Alfian]]></dc:creator>
            <pubDate>Wed, 01 Jul 2020 05:07:11 GMT</pubDate>
            <atom:updated>2020-07-01T05:07:11.739Z</atom:updated>
            <content:encoded><![CDATA[<h3>Handling Multiple View in RecyclerView Adapter Using Visitor Pattern on Android — <em>Part 1/3</em></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*r7z4aCGRHG_WpLZL" /><figcaption>Photo by <a href="https://unsplash.com/@carlevarino?utm_source=medium&amp;utm_medium=referral">Cesar Carlevarino Aragon</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Visitor Pattern, what is it?</h3><p>OOP <em>(Object Oriented Programming) </em>is an important factor to be a capable Android developer. There is an iconic book about Design Pattern in OOP for over 20 years titled <a href="https://springframework.guru/gang-of-four-design-patterns/">Gang of Four</a>. Visitor Pattern belongs to one category named Behavioral Pattern.</p><p>In accordance with Behavioral Pattern role helps define how the object interacts, Visitor Pattern separates the algorithm from an operated object structure by moving the hierarchy of methods to one object called visitor. Anytime a new object operation added, it wouldn’t modify the structure. So, Visitor helps to focus on each job in every element, applying Open/Closed Principle and Single Responsibility Principle from <a href="https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design">S.O.L.I.D Principle</a>.</p><h3>“Okay, I quite got it. So, what problems make Visitor Pattern Exists?”</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*_VPoJ9YxRJ3usPBzaT0ctg.jpeg" /></figure><p>Every early phase of developing the project, it was simple and fun. But it would get more c̶h̶a̶o̶t̶i̶c̶ complex and hard to maintain as we develop larger business logic structure.</p><p>In the case of developing an Android app, of course, we are already familiar with RecyclerView Adapter. It’s okay if we have not so much item view.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a96f9ccda987000b97211d9469c7a4a1/href">https://medium.com/media/a96f9ccda987000b97211d9469c7a4a1/href</a></iframe><p>It’s just 3 ViewHolders, seems little. But as time goes, as team growth, and as business requirement becomes larger, what if we have more and more ViewHolders? Just prepare it as earlier as possible to avoid the riot maintenance because of the boilerplate codes we made.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*vXoIVm43aRNEC1gMXEAV4w.gif" /><figcaption>When our project becomes a riot</figcaption></figure><h3><strong>“Is Visitor Pattern applied only to RecyclerView Adapter?”</strong></h3><p>Because of Visitor Pattern becomes part of Behavioral Pattern, it supports how each related object interacts with keeping these elements in the role without changing its own structure. So, if we have some cases like this below, consider to implement Visitor Pattern.</p><ol><li>New operation needed frequently</li><li>Recurring design problem</li><li>Too much “if else” with a complex algorithm/business logic on each validation</li></ol><h3>How to implement</h3><p>To understand how Visitor Pattern works, we have to know its core method called <a href="https://refactoring.guru/design-patterns/visitor-double-dispatch">Double Dispatch</a>. Let’s take a look at this snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fa45ab9a164e4218c6f842bd46e4ce5/href">https://medium.com/media/8fa45ab9a164e4218c6f842bd46e4ce5/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf85bb74964199771b14b93fa2331879/href">https://medium.com/media/cf85bb74964199771b14b93fa2331879/href</a></iframe><p>Seems pretty good, right? Even Open/Closed Principle has been applied properly. But it’s not implementing Double Dispatch yet. Note that Visitor Pattern has a Double Dispatch to implement it right. So, it’s going to be like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d47bedb63d60fc12598badf7fe86e00/href">https://medium.com/media/6d47bedb63d60fc12598badf7fe86e00/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8dcf83e8f3be016c2572898d0f00172/href">https://medium.com/media/a8dcf83e8f3be016c2572898d0f00172/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc13a8c5bf4476e4270bfb2b70ba2d35/href">https://medium.com/media/bc13a8c5bf4476e4270bfb2b70ba2d35/href</a></iframe><p>As we can see, the difference between single dispatch with double dispatch above is on <strong>the existence of a Visitor</strong> that has a bunch of implementations on each different behavior. When some new CarElements added, we just focus on the Visitor to implement new element behavior. Every CarElement has to <strong>“accept”</strong> an existing Visitor, and the Visitor just declares the implementation based on the related element inside <strong>“visit” </strong>method.</p><h3>Steps:</h3><ol><li>Declare Visitor structure model contains visiting methods for all element members — <strong>(<em>CarElementVisitor.kt)</em></strong></li><li>Create an abstraction of an element. It must have an <strong>accept()</strong> method for the Visitor. If we have another abstraction method besides acceptance, we can add to this as well — <strong>(<em>CarElement.kt)</em></strong></li><li>Add all element members with implementing the acceptance to the visitor. Be aware that the element member should visit on its own class type based on the declared method in Visitor — <strong><em>(Engine.kt, Wheel.kt)</em></strong></li><li>Create the implementation class of the Visitor. Then, declare the <strong>visit()</strong> method implementation for all available elements — <strong><em>(CarElementDoJobVisitor.kt)</em></strong></li><li>The pattern has been created already. Thus, now we can use it in the client app — <strong><em>(Main.kt)</em></strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*ldV0KTowgwKug5b20vueSg.png" /><figcaption>Double Dispatch Workflow</figcaption></figure><p>That’s all for the fundamental theory of Visitor Pattern. Let’s deep dive into the implementation of RecyclerView Adapter on the next part.</p><ol><li>Part I — The Fundamental and Simple Case</li></ol><p><strong>2. </strong><a href="https://toolbox.kurio.co.id/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-2-3-fd39125f4195"><strong>Part II — Implementation to RecyclerView Adapter</strong></a></p><p><strong>3. </strong><a href="https://toolbox.kurio.co.id/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-3-3-b2c9d3a15f35"><strong>Part III — The Enhancement of the Visitor (optional)</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=181b2989b22" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kurio-toolbox/handling-multiple-view-in-recyclerview-adapter-using-visitor-pattern-on-android-part-1-3-181b2989b22">Handling Multiple View in RecyclerView Adapter Using Visitor Pattern on Android — Part 1/3</a> was originally published in <a href="https://medium.com/kurio-toolbox">Kurio Toolbox</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>