2015-08-13 23:54:40 +02:00
---
layout: pattern
title: Proxy
folder: proxy
2015-08-15 18:03:05 +02:00
permalink: /patterns/proxy/
2015-08-20 21:40:07 +02:00
categories: Structural
2021-05-19 10:49:05 -06:00
language: en
2015-09-22 18:25:56 +05:30
tags:
- Gang Of Four
2019-12-13 21:09:28 +02:00
- Decoupling
2015-08-13 23:54:40 +02:00
---
2016-01-03 21:14:30 +01:00
## Also known as
2020-09-01 20:06:47 +03:00
2016-01-03 21:14:30 +01:00
Surrogate
2015-11-04 21:13:32 +02:00
2016-01-03 21:14:30 +01:00
## Intent
2020-09-01 20:06:47 +03:00
Provide a surrogate or placeholder for another object to control access to it.
2015-08-13 23:54:40 +02:00
2017-09-13 21:07:10 +03:00
## Explanation
2020-09-01 20:06:47 +03:00
2021-06-24 15:57:20 +03:00
Real-world example
2017-09-13 21:07:10 +03:00
2020-09-01 20:06:47 +03:00
> Imagine a tower where the local wizards go to study their spells. The ivory tower can only be
> accessed through a proxy which ensures that only the first three wizards can enter. Here the proxy
> represents the functionality of the tower and adds access control to it.
2017-09-13 21:07:10 +03:00
In plain words
> Using the proxy pattern, a class represents the functionality of another class.
Wikipedia says
2020-09-01 20:06:47 +03:00
> A proxy, in its most general form, is a class functioning as an interface to something else.
> A proxy is a wrapper or agent object that is being called by the client to access the real serving
> object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can
> provide additional logic. In the proxy extra functionality can be provided, for example caching
> when operations on the real object are resource intensive, or checking preconditions before
> operations on the real object are invoked.
2017-09-13 21:07:10 +03:00
**Programmatic Example**
2020-09-01 20:06:47 +03:00
Taking our wizard tower example from above. Firstly we have the `WizardTower` interface and the
`IvoryTower` class.
2017-09-13 21:07:10 +03:00
2018-03-28 01:35:43 -04:00
```java
2017-09-13 21:07:10 +03:00
public interface WizardTower {
void enter(Wizard wizard);
}
2021-03-13 13:19:21 +01:00
@Slf4j
2017-09-13 21:07:10 +03:00
public class IvoryTower implements WizardTower {
public void enter(Wizard wizard) {
LOGGER.info("{} enters the tower.", wizard);
}
}
```
2020-09-01 20:06:47 +03:00
Then a simple `Wizard` class.
2017-09-13 21:07:10 +03:00
2018-03-28 01:35:43 -04:00
```java
2017-09-13 21:07:10 +03:00
public class Wizard {
private final String name;
public Wizard(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
```
2020-09-01 20:06:47 +03:00
Then we have the `WizardTowerProxy` to add access control to `WizardTower` .
2017-09-13 21:07:10 +03:00
2018-03-28 01:35:43 -04:00
```java
2021-03-13 13:19:21 +01:00
@Slf4j
2017-09-13 21:07:10 +03:00
public class WizardTowerProxy implements WizardTower {
private static final int NUM_WIZARDS_ALLOWED = 3;
private int numWizards;
private final WizardTower tower;
public WizardTowerProxy(WizardTower tower) {
this.tower = tower;
}
@Override
public void enter(Wizard wizard) {
if (numWizards < NUM_WIZARDS_ALLOWED ) {
tower.enter(wizard);
numWizards++;
} else {
LOGGER.info("{} is not allowed to enter!", wizard);
}
}
}
```
2020-09-01 20:06:47 +03:00
And here is the tower entering scenario.
2017-09-13 21:07:10 +03:00
2018-03-28 01:35:43 -04:00
```java
2020-01-16 11:36:36 +05:30
var proxy = new WizardTowerProxy(new IvoryTower());
2020-09-01 20:06:47 +03:00
proxy.enter(new Wizard("Red wizard"));
proxy.enter(new Wizard("White wizard"));
proxy.enter(new Wizard("Black wizard"));
proxy.enter(new Wizard("Green wizard"));
proxy.enter(new Wizard("Brown wizard"));
```
Program output:
```
Red wizard enters the tower.
White wizard enters the tower.
Black wizard enters the tower.
Green wizard is not allowed to enter!
Brown wizard is not allowed to enter!
2017-09-13 21:07:10 +03:00
```
2015-08-13 23:54:40 +02:00
2019-12-07 20:01:13 +02:00
## Class diagram
2020-09-01 20:06:47 +03:00
2019-12-07 20:01:13 +02:00

2016-01-03 21:14:30 +01:00
## Applicability
2020-09-01 20:06:47 +03:00
Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an
object than a simple pointer. Here are several common situations in which the Proxy pattern is
applicable.
2015-08-13 23:54:40 +02:00
2016-11-23 22:03:26 +02:00
* Remote proxy provides a local representative for an object in a different address space.
* Virtual proxy creates expensive objects on demand.
2020-09-01 20:06:47 +03:00
* Protection proxy controls access to the original object. Protection proxies are useful when
objects should have different access rights.
2015-08-13 23:54:40 +02:00
2021-06-24 15:57:20 +03:00
Typically, the proxy pattern is used to
2015-08-13 23:54:40 +02:00
2016-11-23 22:03:26 +02:00
* Control access to another object
* Lazy initialization
* Implement logging
* Facilitate network connection
* Count references to an object
2015-08-13 23:54:40 +02:00
2017-01-21 13:37:16 +02:00
## Tutorials
2019-12-13 21:09:28 +02:00
2017-01-21 13:37:16 +02:00
* [Controlling Access With Proxy Pattern ](http://java-design-patterns.com/blog/controlling-access-with-proxy-pattern/ )
2020-08-04 21:35:41 +03:00
## Known uses
2015-08-13 23:54:40 +02:00
* [java.lang.reflect.Proxy ](http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Proxy.html )
2015-08-15 18:03:05 +02:00
* [Apache Commons Proxy ](https://commons.apache.org/proper/commons-proxy/ )
2020-09-01 20:06:47 +03:00
* Mocking frameworks [Mockito ](https://site.mockito.org/ ),
[Powermock ](https://powermock.github.io/ ), [EasyMock ](https://easymock.org/ )
2015-09-22 18:25:56 +05:30
2020-08-04 21:35:41 +03:00
## Related patterns
* [Ambassador ](https://java-design-patterns.com/patterns/ambassador/ )
2016-01-03 21:14:30 +01:00
## Credits
2015-09-22 18:25:56 +05:30
2020-07-06 13:31:07 +03:00
* [Design Patterns: Elements of Reusable Object-Oriented Software ](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59 )
2020-07-07 18:05:11 +03:00
* [Head First Design Patterns: A Brain-Friendly Guide ](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b )