Author: Vijay SRJ

  • How to implement Observer pattern in Java?

    Let’s say my blog is gaining popularity. A lot of people want to be updated with my posts. And let’s assume that my current blog platform doesn’t provide this option. So I am writing an application to update my followers. For now I am considering two types of followers: Email Followers WhatsApp Followers Whenever I…

  • How to implement State Pattern in Java?

    Let’s say you run an eCommerce store. You want to develop an application to run your eCommerce. You are specifically concerned about the delivery of order items. When a customer places an order it goes through many different states: Order is received Order is packed Order is shipped Order is in transit Order is delivered…

  • How to implement Strategy Pattern in Java?

    Let’s say you run a eCommerce Shop. You wan’t to adopt a different pricing strategy at different periods of the year. Say , during Christmas you give a 40% discount on all items, During periods where you run out of stock you add 20% to the existing stock price. And during the other times you…

  • How to implement Template Method pattern in Java?

    Let’s say you have created an application with two complex forms : One for managing products. Another for managing user of the products. You have a single form ‘ProductForm’ for managing a product and a single form ‘UserForm’ for managing a user. You want to perform three levels of validation for both the forms: Input…

  • How to implement Mediator pattern in Java?

    Let’s say you want to automate posting on social media. You want to update your twitter and blog feed automatically when you post on facebook. Similarly if you post on any other media, the other two media should get the feed automatically updated. Let’s represent this problem in Java. You can have a class for…

  • How to implement Command pattern in Java?

    Let’s say you run a company. You want to give commands to your manager to instruct developers to write code for an application and testers to test it. Also you want to tell the manager what to do dynamically. You give him only the command but the manager neither knows who is going to execute…

  • How to implement Chain of Responsibility pattern in Java?

    Let’s say you run a hospital. And you are developing an application to handle patients. You have created a process: All patients are first attend by a junior doctor. The junior doctor checks the symptoms and if the severity is low , she treats them , else she sends the patient to the senior doctor.…

  • How to implement Proxy pattern in Java?

    Let’s say you want to load a file from a server. There is an API available which does the actual action of loading the file. But calling it every time overloads the server. Instead you can introduce a proxy in between which will fetch the file the first time and then put it on cache.…

  • How to implement Flyweight pattern in Java?

    Let’s say you are an artist. You want to create different Color Palettes using three colors at a time. The three colors can be anything of all colors know to us. Let’s represent this in Java code using Flyweight pattern and check what problems it solves. Flyweight pattern let’s you reuse objects instead of creating…

  • How to implement Facade pattern in Java?

    Facade is one of the easiest patterns to implement. And if one has not come across the “Gang of Four Design Patterns” they probably already would have used facade pattern. Facade just provides a single interface to a set of sub systems. Let me explain. Let’s say you are writing an application for an eCommerce…