Tag: gangoffour

  • How to implement Visitor pattern in Java?

    Let’s say you are running a shop. You sell books, phones , shirts and other stuff. You already have a software to manage your stock. You store the original price, selling price , quantity and name of each item in your database. One day you decide to launch a massive discount sales. And you want…

  • How to implement Interpreter pattern in Java?

    Let’s say you are writing code for an application. You have decided to use only plain JDBC to peform database operations. No ORM (Object Relational Mapping tools). And so you write plain queries in your application code. When you run your code , your application starts throwing Query Syntax errors. You go back to the…

  • How to implement Memento pattern in Java?

    Let’s say you are designing a shirt online using a tool. You have created a design and saved it. And suddenly you realize you want the old design back. You find a menu to revert , you click it and you get back your old design. Let’s get under the hoods and see how this…

  • 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 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 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 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…

  • How to implement decorator pattern in Java?

    Let’s say you are designing a house, an apartment house. You have done the base design. And now you want to add some more to your design. Let’s do that using Decorator pattern. A decorator pattern lets you add responsibilities to an existing object without changing its existing functionality. An alternate way of achieving this…