Posts

Showing posts with the label design patterns

Adapter Design Pattern in Java with Example

Adapter design pattern in Java, also known as the Wrapper pattern is another very useful GOF pattern, which helps to bridge the gap between two classes in Java. As per the list of Gang of Four patterns, Adapter is a structural pattern, much like Proxy, Flyweight, Facade, and Decorator pattern in Java. As the name suggest, Adapter allows two classes of a different interface to work together, without changing any code on either side . You can view Adapter pattern is a central piece of the puzzle, which joins two pieces, which can not be directly joined because of different interfaces. I see a couple of reference of Adapter design pattern in one of my favorite book Clean Code , but the idea is very well explained in Head First Design Pattern , the image which they show to illustrate Adapter design pattern is worth all the talk. Read more �

Java Object Oriented Analysis and Design Problem - Vending Machine Part 2

This is the second part of Java tutorial to show how to create Vending Machine in Java. In the first part, we have discussed problem statement and the solution itself, but unit testing and design document was still pending, which we'll see in this article. As I said, there are multiple ways to implement Vending machine in Java e.g. you could have easily used state design pattern to implement a vending machine, in fact, it's one of the best examples of State design pattern. Vending machine behaves differently on different states e.g. return a product if the machine is not empty, otherwise, it just returns coins, so it ideally fits in state design pattern. Though, In our solution, I have not used the state design pattern but have just coded the solution with an if else block. This is easier because of a limited number of state and not many state transition but in more real world scenario, state design pattern is better as it uses Polymorphism and removes the logic we have put ins...

Command design Pattern in Java with Example

In simple words, Command design pattern is used to separate a request for an action from the object which actually performs the action . This decoupling between Invoker and Receiver object provides a uniform ways to perform different types of actions. This decoupling is achieved using a Command object , which is usually an interface with methods like execute() . The Requestor or Invoker only knows about Command object, and doesn't care of the actual object which process the request, which can be different. This transparency leads clean code on Invoker side and also enables opportunity to do several smart things on Command side. Your Command object can be as dumb as simply delegating request to Receiver and can be as smart as recording last command for performing UNDO and REDO functionality. Read more �

Difference between Singleton Pattern vs Static Class in Java

Singleton pattern vs Static Class (a class, having all static methods) is another interesting questions, which I missed while blogging about Interview questions on Singleton pattern in Java . Si nce both Singleton pattern and static class provides good accessibility, and they share some similarities e.g. both can be used without creating object and both provide only one instance, at very high level it looks that they both are intended for same task. Because of high level similarities, interviewer normally ask questions like, Why you use Singleton instead of Static Methods, or Can you replace Singleton with static class, and what are differences betwee n Singleton pattern and static in Java . In order to answer these question, it�s important to remember fundamental difference between Singleton pattern and static class, former gives you an Object , while later just provide static methods. Since an object is always much more capable than a method, it can guide you when to use Sing...

Difference between Factory and Abstract Factory design pattern in Java

Both Abstract Factory and Factory design pattern are creational design pattern and use to decouple clients from creating object they need, But there is a significant difference between Factory and Abstract Factory design pattern, Factory design pattern produces implementation of Products e.g. Garment Factory produce different kinds of clothes, On the other hand Abstract Factory design pattern adds another layer of abstraction over Factory Pattern and Abstract Factory implementation itself e.g. Abstract Factory will allow you to choose a particular Factory implementation based upon need which will then produce different kinds of products. In short 1) Abstract Factory design pattern creates Factory 2) Factory design pattern creates Products Read more �

How to create thread safe Singleton in Java - Java Singleton Example

Thread safe Singleton means a Singleton class which returns exactly same instance even if exposed to multiple threads. Singleton in Java has been a classical design pattern like Factory method pattern or Decorator design pattern and has been used a lot even inside JDK like java.lang.Runtime is an example of Singleton class. Singleton pattern ensures that exactly one instance of class will remain in Java program at any time. In our last post 10 Interview questions on Singleton in Java we have discussed many different questions asked on Singleton pattern, One of them was writing Thread safe singleton in Java. Prior to Java 5 double checked locking mechanism is used to create thread-safe singleton in Java which breaks if one Thread doesn't see instance created by other thread at same time and eventually you will end up with more than one instance of Singleton class. From Java 5 onwards volatile variable guarantee can be used to write thread safe singleton by using double checked...

Inversion of Control and Dependency Injection design pattern with real world Example - Spring tutorial

Inversion of Control and Dependency Injection is a core design pattern of Spring framework. IOC and DI design pattern is also a popular design pattern interview question in Java . As the name suggest Inversion of control pattern Inverts responsibility of managing the life cycle of the object e.g. creating an object, setting their dependency etc from application to a framework, which makes writing Java application even more easy. The programmer often confused between IOC and DI, well both words used interchangeably in Java world but Inversion of Control is a more general concept and Dependency Injection is a concrete design pattern. Spring framework provides two implementations of IOC container in the form of Application Context and BeanFactory which manages life-cycle of bean used by Java application. As you may know, necessity is the mother of invention, it benefits to first understand problem solved by IOC and Dependency Injection design pattern. This makes your understanding mor...

Builder Design pattern in Java - Example Tutorial

Builder design pattern in Java is a creational pattern i.e. used to create objects, similar to factory method design pattern which is also creational design pattern. Before learning any design pattern I suggest find out the problem a particular design pattern solves. Its been well said necessity is mother on invention . learning design pattern without facing problem is not that effective, Instead if you have already faced issues than its much easier to understand design pattern and learn how its solve the issue. In this Java design pattern tutorial we will first see what problem Builder design pattern solves which will give some insight on when to use builder design pattern in Java, which is also a popular design pattern interview question and then we will see example of Builder design pattern and pros and cons of using Builder pattern in Java. Read more �