Tag: java

  • What are the different ways to group a collection in Java?

    Let’s say you have a collection of phones. The phones have the attributes : model name , phone type (Android or Iphone) , cost , color and rating. And you want to group the phones based on whether they are Iphones or Android Phones. You want to find the costliest IPhone and the costliest Android…

  • How to implement Concurrency in Java – A very minimalist example

    Let’s say you are implementing a batch service and you want to execute a lot of methods. Let’s assume they are independent functionalities and the output of one is not used by any other methods. Executing them one by one will consume a lot of time. The best strategy is to execute them in parallel.…

  • How to convert LocalDate to Util Date and vice versa in Java?

    Java 8 introduced LocalDate to represent date without the time information . But sometimes we may need to convert LocalDate to the legacy java util Date in our code . This post explains how to convert from LocalDate to java.util.Date and vice versa. The below code converts local date to Util Date: And the below…

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