Category: java

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

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

  • How to implement Composite pattern in Java?

    Let’s say you are the manager of a project. You have been asked to calculate the total cost to your company of your entire team. You have a service development team with two members. You have a UI development team with two members. You have a Quality Assurance team with two members. You have a…