Category: design patterns

  • How to implement SAGA design pattern in Spring Boot?

    How to implement SAGA design pattern in Spring Boot?

    Contents: Problem: Microservices come with their own advantages and disadvantages. One such disadvantage is managing distributed transactions. Let’s say your transaction spans 4 different microservices. How do you ensure that your transaction either commits successfully with all the 4 tasks succeeding or fails successfully if any of the tasks is not completed ( the completed…

  • How to implement CQRS design pattern in Spring Boot?

    How to implement CQRS design pattern in Spring Boot?

    Contents Problem: Let’s say you run a large scale ecommerce store. You have a large user base who query your system for products much more than they buy them. In other words , your system has more read requests than write requests. And so you would like to handle the high load of read requests…

  • How to implement Event Sourcing in Spring Boot?

    How to implement Event Sourcing in Spring Boot?

    All our web applications deal with data. We store this data mostly in a database. The data which is stored thus represents the current state of the data. For example , if you have a Customer table the table will have entries which represent the current state of the customer (what their name is and…

  • How to implement Transactional Outbox design pattern in Spring Boot Microservices?

    How to implement Transactional Outbox design pattern in Spring Boot Microservices?

    Let’s say you have created a microservice. One of the APIs in the microservice does two operations: How can you make sure both are transactional? In other words if database update fails don’t send the message to the other service and if message sending fails rollback the database update? In Spring you handle transactions using…

  • 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 Iterator pattern in java?

    Let’s say you run a small theatre. Your theatre is unique , only those who have membership can watch movies in your theatre. Anyone of any age can be a member. You are not just screening latest movies but curated collections tailored for movie lovers. You have a database of all members . There are…

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