Hello guys, if you are preparing for Java interviews and looking for difference between Proxy and State design pattern then you have come at the right place. In the past, I have explained several important object oriented design patterns like State patttern, Strategy, Adapter, Facade, Factory, Observer, Decorator, Command, and Composite Pattern and also shared frequently asked questions on design patterns (See here) and in this article, I am going to share the key difference between Proxy and State Design Pattern in Java and OOP. The difference between Proxy and State pattern comes in both intent and structure, here are the key difference between both Proxy and State Design Pattern in Object oriented programming.
Difference between Proxy and State design Pattern in Java
Here are key differences between State and Proxy Design Patterns in Java or any other object oriented programming language:
2. Proxy is used to control access of the real class while state pattern allows you to change state dynamically and allow your class to behave differently at different state.
3. State pattern not only adds more implementation to Proxy but also ability to switch from one implementation (state) to another during lifetime of surrogate (the object whose state is changing).
4. It's not necessary for the actual class to have the same interface as Proxy, as long as Proxy is somehow representing for actual class, but this goes against the definition of Proxy pattern given by GOF. However, it's better to have both Proxy and real class implement same interface so that actual class have all the methods which proxy needs to call.
5) Another difference between Proxy and State patterns is in the problems they solved. Proxy pattern can be used in different flavor e.g.
Remote Proxy : act as proxy for an object in different address space e.g. in separate JVM. If you have used RMI in Java then you know that a remote proxy is automatically created for you by RMI compiler (rmic) when it creates stubs and skeletons.
Virtual Proxy : Can be used to facilitate "lazy initialization" for expensive objects. Initially proxy is returned without heavy stuff, which is created only when needed.
Protection Proxy : used when you don't want to expose full functionality of object to it's client. This is the true access control restriction example of proxy pattern.
Proxy Pattern
State Design Pattern
Here is the UML diagram of State design pattern in Java which explains how it works and how its client uses it to manage state.Similarities between Proxy and State Design Pattern
Now that you have seen the difference between Proxy and State Pattern, let's take a look at similarities between them to better understand them:
2) In both cases, surrogate class is derived from based class along with other implementation, though due to different reasons. In case of Proxy pattern, proxy class impalements same interface as actual class so that it can stand in place of it. So that you can pass proxy to a method which is accepting actual class or you can return an instance of Proxy instead of actual class.
That's all about the difference between State and Proxy Pattern in Java or any other object oriented programming language. I agree this is quite a short article but I wanted to keep it to the point, if you like this kind of short article and let me know in comments. And, if you want more explanation and example and longer article then also share your thoughts. I like to create content which is both helpful and easy to digest.
All the best with your learning
- How to implement Template Design Pattern in Java (Template Pattern)
- Real world example of Strategy Pattern in Java (strategy pattern demo)
- 5 Free Courses to learn Object Oriented Programming (courses)
- How to design a Vending Machine in Java? (questions)
- How to implement a Decorator design pattern in Java? (tutorial)
- 18 Java Design Pattern Interview Questions with Answers (list)
- When to use Command Design Pattern in Java (example)
- 20 System Design Interview Questions (list)
- Difference between Factory and Dependency Injection Pattern? (answer)
- 7 Best Courses to learn Design Pattern in Java (courses)
- Difference between State and Strategy Design Pattern in Java? (answer)
- How to create thread-safe Singleton in Java (example)
- Difference between Factory and Abstract Factory Pattern? (example)
- 7 Books to learn System Design for Beginners (System design books)
- When to use Visitor pattern in Java? (visitor pattern)
- 5 Free Courses to learn Data Structure and Algorithms (courses)
- How to use Factory method design pattern in Java? (tutorial)
- 7 Best Books to learn the Design Pattern in Java? (books)
- How to use Composite Design Pattern in Java (composite example)
Thanks for reading this article so far. If you like this article and my explanation of difference between Proxy and State Design Pattern then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you want to learn design patterns in depth and looking for resources then you can also checkout these best Java Design pattern courses to start with. This article contains best design pattern courses for Java developers from Udemy, Coursera, and Pluralsight. They are specially good for intermediate and experienced Java developers.
No comments:
Post a Comment