Proxy Pattern
In the Proxy Pattern (Proxy Pattern), one class represents the functionality of another class; this type of design pattern belongs to structural patterns.
The Proxy Pattern controls access to the original object by introducing a proxy object. The proxy object acts as an intermediary between the client and the target object, responsible for forwarding the client's requests to the target object, while it can perform additional processing before and after forwarding the request.
In the Proxy Pattern, we create an object with an existing object in order to provide a functional interface to the outside world.
Introduction
Intent
Provide a proxy for another object to control access to this object.
Main problem solved
- The Proxy Pattern solves problems that may be encountered when directly accessing certain objects, such as high object creation cost, the need for security control, or remote access.
Use Cases
- When you need to exercise some control or perform extra processing when accessing an object.
Implementation
- Add an intermediate layer: Create a proxy class that acts as an intermediate layer for the real object.
- Proxy combined with the real object: The proxy class holds a reference to the real object and exercises control during access.
Key code
- Proxy class: Implements the same interface as the real object and adds extra control logic.
- Real object: The object that actually performs the task.
Application Example
- Shortcut: Shortcuts in Windows systems act as proxies for files or programs.
- Role playing: Sun Wukong acts as a proxy for Gao Cuilan, and Zhu Bajie cannot tell the difference.
- Ticket agency: When buying train tickets, the ticket agency acts as a proxy for the railway station.
- Check: Acts as a proxy for funds in a bank account, controlling access to the funds.
- Spring AOP: Use the Proxy pattern to implement aspect-oriented programming.
Advantages
- Separation of responsibilities: The Proxy pattern separates access control from business logic.
- Extensibility: Additional functions or controls can be added flexibly.
- Intelligence: Can intelligently handle access requests, such as lazy loading, caching, etc.
Disadvantages
- Performance overhead: Adding a proxy layer may affect the processing speed of requests.
- Implementation complexity: Some types of the Proxy pattern may be relatively complex to implement.
Usage recommendation
- Choose the appropriate proxy type according to specific needs, such as remote proxy, virtual proxy, protection proxy, etc.
- Ensure that the proxy class is consistent with the real object's interface so that clients can use the proxy transparently.
Notes
- Difference from the Adapter pattern: The Adapter pattern changes the interface, while the Proxy pattern does not.
- Difference from the Decorator pattern: The Decorator pattern is used to enhance functionality, while the Proxy pattern is used to control access.
structures
Mainly involves the following core roles:
-
Abstract Subject:
- Defines a common interface for the real subject and the proxy subject, so that the proxy subject can be used wherever the real subject is used.
-
Real Subject:
- Implements the abstract subject interface and is the real object represented by the proxy object. The client accesses the real subject directly, but in some cases it can be accessed indirectly through the proxy subject.
-
Proxy:
- Implements the abstract subject interface and holds a reference to the real subject. The proxy usually provides some additional functions on top of the real subject, such as lazy loading, access control, logging, etc.
-
Client (Client):
- Use the abstract subject interface to operate on the real subject or the proxy subject without needing to know which implementation class it is.
Create a class that implements
We will create a Image interface as the bridge implementation and concrete classes that implement the Image interface.ProxyImage is a proxy class that reduces RealImage the memory usage of object loading.
ProxyPatternDemo Class uses ProxyImage to obtain the object to be Image loaded, and display it as needed.
Step 1
Create an interface.
Image.java
public
interface
Image
{
void
display
(
)
;
}
Step 2
Create concrete classes that implement the interface.
RealImage.java
public
class
RealImage
implements
Image
{
private
String
fileName
;
public
RealImage
(
String
fileName
)
{
this
.
fileName
=
fileName
;
loadFromDisk
(
fileName
)
;
}
@
Override
public
void
display
(
)
{
System
.
out
.
println
(
"
Displaying
"
fileName
)
;
}
private
void
loadFromDisk
(
String
fileName
)
{
System
.
out
.
println
(
"
Loading
"
fileName
)
;
}
}
ProxyImage.java
public
class
ProxyImage
implements
Image
{
private
RealImage
realImage
;
private
String
fileName
;
public
ProxyImage
(
String
fileName
)
{
this
.
fileName
=
fileName
;
}
@
Override
public
void
display
(
)
{
if
(
realImage
==
null
)
{
realImage
=
new
RealImage
(
fileName
)
;
}
realImage
.
display
(
)
;
}
}
Step 3
When requested, use ProxyImage to get RealImage the object of the class.
ProxyPatternDemo.java
public
class
ProxyPatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
Image
image
=
new
ProxyImage
(
"
test_10mb.jpg
"
)
;
//
The image will be loaded from disk
image
.
display
(
)
;
System
.
out
.
println
(
"
"
)
;
//
The image does not need to be loaded from disk
image
.
display
(
)
;
}
}
Step 4
Run the program, output result:
Loading test_10mb.jpg
Displaying test_10mb.jpg
Displaying test_10mb.jpg