Java Design Pattern Interview Question

Connect with

Oracle Java
Java Design pattern is one o the frequently asked interview questions nowadays specially more than 5 years of experience.

When to use Strategy Design Pattern in Java?

Strategy pattern in quite useful for implementing set of related algorithms e.g. different compression algorithms, different way of logger etc. Strategy pattern used object composition, which allow to switch algorithm run time.

This pattern follow open closed design principle and quite useful in Java. One example of Strategy pattern from JDK itself is a Collections.sort() method and Comparator interface, which is a strategy interface and defines strategy for comparing objects.

Difference between Strategy and State design Pattern in Java?

This is an interesting Java design pattern interview questions , why this is interesting because both Strategy and State pattern has same structure it means both UML diagram looks exactly same. UML class diagram for both pattern look exactly same, but there intent is totally different.

State design pattern is used to define and mange state of object, while Strategy pattern is used to define a set of interchangeable algorithm and let’s client to choose one of them. You can say Strategy pattern is a client driven pattern while Object can manage there state itself.

Does each subsystem have only one facade?

it’s not necessarily, The pattern certainly allow for any number of facade to be created for a given subsystem.

is there any way , client can know about proxy class or not?

Yes, the proxy class has a static method called isProxyClass(). this method returns true if class is a dynamic proxy class. other than class act as normal class which implement a interface.

Connect with

Leave a Reply

Your email address will not be published. Required fields are marked *