Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

Difference between Proxy and State design Pattern in Java

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


1. Structurally, main difference between them is that Proxy pattern only has one implementation, while state has more than one implementation to represent different states.

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


Difference between Proxy and State design pattern




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:

1) Proxy and State pattern are both example of surrogate pattern, i.e. they both provide a fronting class which delegates the work to real class, which is hidden behind surrogate.

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


No comments :

Post a Comment