<?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 Ankit Thakur on Medium]]></title>
        <description><![CDATA[Stories by Ankit Thakur on Medium]]></description>
        <link>https://medium.com/@ankitccb?source=rss-3d196de88342------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*sYydRL_JMkoW2SVR.</url>
            <title>Stories by Ankit Thakur on Medium</title>
            <link>https://medium.com/@ankitccb?source=rss-3d196de88342------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 04:51:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ankitccb/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[Thinking Generically]]></title>
            <link>https://medium.com/@ankitccb/thinking-generically-e7cbbf13e365?source=rss-3d196de88342------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7cbbf13e365</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[solid-principles]]></category>
            <category><![CDATA[generics]]></category>
            <dc:creator><![CDATA[Ankit Thakur]]></dc:creator>
            <pubDate>Sat, 25 Feb 2023 13:39:39 GMT</pubDate>
            <atom:updated>2023-02-26T06:41:08.875Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*YwvcdCHDwjFgVTV-26w1DA.jpeg" /></figure><p>There are so many coding patterns to achieve loose coupling and writing clean and maintainable code. Coding to Interface, Inheritance, Polymorphism and Composition — most of these are covered within <strong>SOLID</strong> principles, Software Design Patterns and Generics.</p><p>Often when we get down to implementing a given use case, we try to retrofit a lot of the above patterns to reap the benefits. I have experienced over time that not always will you be able to apply the above techniques to deliver the use case. You may not even need these for some of them. The best way to use these patterns is to let them extend themselves naturally to the problem that you are solving.</p><p>Consider the use case where you have to maintain an audit log for an E-Commerce application, wherein you wish to log the incoming details and outbound responses, in addition to processing the actual request. Precisely, you need to do the following:</p><blockquote>Persist the request.</blockquote><blockquote>Process the request.</blockquote><blockquote>Persist the response.</blockquote><p>An E-Commerce Application consists of many microservices, but for our use case, let’s consider the <strong>Order Service</strong> and <strong>Shipping Service. </strong>Each of these services will have some logic to process the request.</p><p>A crude way of achieving our goals would be to add two methods to persist the request and response to both (and all) of these microservices and wrap the requesting processing logic around these. The code for the Order Service would look something like the below:</p><pre>public class OrderService {<br>  // some member variables<br>  // Dependency injection logic<br>  public OrderCreated processRequest(CreateOrder request) {<br>    persistRequest(request);<br>    // process the order and get response<br>    persistResponse(response);<br>    return response;<br>  }<br><br>  private void persistRequest(CreateOrder request) {<br>  }<br><br>  private void persistResponse(OrderCreated response) {<br>  }<br>}</pre><p>And the code for the Shipping Service would be:</p><pre>public class ShippingService {<br>  // some member variables<br>  // Dependency injection logic<br>  public OrderShipped processRequest(ShipOrder request) {<br>    persistRequest(request);<br>    // process the order and get response<br>    persistResponse(response);<br>    return response;<br>  }<br><br>  private void persistRequest(ShipOrder request) {<br>  }<br><br>  private void persistResponse(OrderShipped response) {<br>  }<br>}</pre><p>Do you spot the problems with the above code?</p><blockquote>The existing method is being modified, violating the Open Close principle.</blockquote><blockquote>We are not enforcing a contract for a new service that needs to be audited to have the persistRequest and persistResponse functionality.</blockquote><p>Without going very far, the first pattern that comes to mind is the <strong>Template Design Pattern.</strong></p><blockquote>The template design pattern is a behavioral design pattern that defines the basic steps of an algorithm in a superclass while allowing subclasses to override specific steps without changing the overall algorithm structure.</blockquote><p>We can create an abstract class that has a method that wraps the <strong>processRequest </strong>functionality with the <strong>pesistRequest</strong> and <strong>persistResponse</strong>.</p><pre>public interface EcommerceBase {<br>  OrderCreated processRequest(CreateOrder request);<br>  OrderShipped processRequest(ShipOrder request);<br>}</pre><p>Right away, the problem we see here, is that there are two methods defined to do the same job.</p><p>Enter Generics.</p><pre>public interface EcommerceBase&lt;T, U&gt; {<br>  U processRequest(T request);<br>} </pre><p>Now, we have a single method that can be implemented by services to process orders in an extensible manner, but where is the contract enforcement happening, you ask?</p><pre>public abstract class AuditableService&lt;T, U&gt; implements ECommerceBase&lt;T, U&gt; {<br>  protected ECommerceBase&lt;T, U&gt; baseEcommerceService;<br><br>  public AuditableService(ECommerceBase&lt;T, U&gt; baseEcommerceService) {<br>    this.baseEcommerceService = baseEcommerceService;<br>  }<br><br>  public U processRequest(T request) {<br>    persistRequest(request);<br>    U response = baseECommericeService.processRequest(t);<br>    processResponse(response);<br>    return response;<br>  }<br><br>  protected abstract void persistRequest(T request);<br><br>  protected abstract void persistResponse(U response);<br>}</pre><p>And now, the service classes would simply extend the <em>AuditableService </em>above to enforce the two methods and follow a consistent pattern.</p><pre>public class OrderService implements ECommerceBase&lt;CreateOrder, OrderCreated&gt; {<br>  // some member variables<br>  // Dependency injection logic<br><br>  @Override<br>  public OrderCreated processRequest(CreateOrder request) {<br>    // process request<br>  }<br>}</pre><pre>public class AuditableOrderService extends AuditableService&lt;CreateOrder, OrderCreated&gt;{<br>  // some member variables<br><br>  public AuditableOrderService(ECommerceBase&lt;Create, OrderCreated&gt; eCommerceService) {<br>    super(eCommerceService);<br>  }<br><br>  void persistRequest(CreateOrder request) {<br>    // custom request persistence logic<br>  }<br><br>  void persistResponse(OrderCreated response) {<br>    // custom response persistence logic<br>  }<br>}</pre><p>Now, you can inject the <em>OrderService</em> within <em>AuditableOrderService</em> and swap out the <em>OrderService</em> call in the resource layer with the <em>AuditableOrderService</em>.</p><pre>@Path(&#39;/v1/api&#39;)<br>public class ECommerceResource {<br>  <br>  private AuditableOrderService auditableOrderService;<br>  <br>  public ECommerceResource() {<br>    this.auditableOrderService = new AuditableOrderService(new OrderService());<br>  }<br>  <br>  @POST<br>  @Consumes(MediaType.APPLICATION_JSON)<br>  public OrderCreated processRequest(CreateOrder request) {<br>    return auditableOrderService.processRequest(request);<br>  }<br>}</pre><p>And just like that, we have implemented another design pattern — decorater.</p><blockquote>The Decorator pattern is a structural design pattern that allows adding new behavior to an object dynamically by wrapping it with another object that adds this behavior.</blockquote><p>Let me know your thoughts in the comments!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7cbbf13e365" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>