Skip to content

Commit

Permalink
docs: update proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed May 18, 2024
1 parent 57f7c39 commit 551e354
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ title: Proxy
category: Structural
language: en
tag:
- Gang Of Four
- Decoupling
- Decoupling
- Encapsulation
- Gang Of Four
- Proxy
- Security
- Wrapping
---

## Also known as
Expand All @@ -19,41 +23,30 @@ Provide a surrogate or placeholder for another object to control access to it.

Real-world example

> 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.
> 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.
In plain words

> Using the proxy pattern, a class represents the functionality of another class.
Wikipedia says

> 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.
> 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.
**Programmatic Example**

Taking our wizard tower example from above. Firstly we have the `WizardTower` interface and the
`IvoryTower` class.
Taking our wizard tower example from above. Firstly we have the `WizardTower` interface and the `IvoryTower` class.

```java
public interface WizardTower {

void enter(Wizard wizard);
}

@Slf4j
public class IvoryTower implements WizardTower {

public void enter(Wizard wizard) {
LOGGER.info("{} enters the tower.", wizard);
}

}
```

Expand Down Expand Up @@ -126,44 +119,51 @@ Brown wizard is not allowed to enter!

## Class diagram

![alt text](./etc/proxy.urm.png "Proxy pattern class diagram")
![Proxy](./etc/proxy.urm.png "Proxy pattern class diagram")

## Applicability

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.

* Remote proxy provides a local representative for an object in a different address space.
* Virtual proxy creates expensive objects on demand.
* Protection proxy controls access to the original object. Protection proxies are useful when
objects should have different access rights.

Typically, the proxy pattern is used to
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. Typically, the proxy pattern is used to

* Control access to another object
* Lazy initialization
* Implement logging
* Facilitate network connection
* Count references to an object
* Provide a local representation for an object that is in a different address space.

## Tutorials

* [Controlling Access With Proxy Pattern](http://java-design-patterns.com/blog/controlling-access-with-proxy-pattern/)

## Known uses
## Known Uses

* Virtual Proxies: In applications that need heavy resources like large images or complex calculations, virtual proxies can be used to instantiate objects only when needed.
* Remote Proxies: Used in remote method invocation (RMI) to manage interactions with remote objects.
* Protection Proxies: Control access to the original object to ensure proper authorization.
* [java.lang.reflect.Proxy](http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Proxy.html)
* [Apache Commons Proxy](https://commons.apache.org/proper/commons-proxy/)
* Mocking frameworks [Mockito](https://site.mockito.org/),
[Powermock](https://powermock.github.io/), [EasyMock](https://easymock.org/)
* Mocking frameworks [Mockito](https://site.mockito.org/),[Powermock](https://powermock.github.io/), [EasyMock](https://easymock.org/)
* [UIAppearance](https://developer.apple.com/documentation/uikit/uiappearance)

## Related patterns
## Consequences

Benefits:

* Controlled Access: Proxies control access to the real object, allowing for checks, logging, or other operations.
* Lazy Initialization: Proxies can delay the creation and initialization of resource-intensive objects until they are needed.
* Remote Proxy Handling: Simplifies interaction with remote objects by handling the network communication.

Trade-offs:

* Overhead: Adding a proxy introduces additional layers that might add overhead.
* Complexity: Increases the complexity of the system by adding more classes.

## Related Patterns

* [Ambassador](https://java-design-patterns.com/patterns/ambassador/)
* [Adapter](https://java-design-patterns.com/patterns/adapter/): The Adapter pattern changes the interface of an existing object, whereas Proxy provides the same interface as the original object.
* [Ambassador](https://java-design-patterns.com/patterns/ambassador/): Ambassador is similar to Proxy as it acts as an intermediary, especially in remote communications, enhancing access control and monitoring.
* [Decorator](https://java-design-patterns.com/patterns/decorator/): Both Decorator and Proxy patterns provide a level of indirection, but the Decorator pattern adds responsibilities to objects dynamically, while Proxy controls access.
* [Facade](https://java-design-patterns.com/patterns/facade/): Facade provides a simplified interface to a complex subsystem, while Proxy controls access to a particular object.

## Credits

* [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)
* [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)
* [Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3yhh525)

0 comments on commit 551e354

Please sign in to comment.