Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
An overview of the
Also known as the delegate pattern, the structural design pattern can provide a substitute for the object, control the access to the original object, is a very high usage pattern.
role
Abstract topic: Declares the interface of the service that the proxy class must follow.
Service subject: A service class, which is a propitious party, provides a service interface to the actual business logic.
Proxy subject: The proxy class, which contains a reference member variable to a service object, is responsible for creating or deleting a service object as needed, and the proxy performs its tasks, typically pre-processing and post-processing before and after the service class.
The template
Abstract interfaces: Declare the functions that proxy classes and service classes must implement.
public interface IGamePlayer {
/** ** ** /
public void killBoss(a);
/** ** */
public void clearance(a);
}
Copy the code
Service class: The actual service processing logic
public class GamePlayer implements IGamePlayer{
private String name;
public GamePlayer(String name) {
this.name = name;
}
@Override
public void killBoss(a) {
System.out.println(this.name + "Three dozen of them!");
}
@Override
public void clearance(a) {
System.out.println(this.name + "Killed the white-bone spirit!"); }}Copy the code
Proxy classes: Proxy service classes
public class GamePlayerProxy implements IGamePlayer{
private IGamePlayer player;
public GamePlayerProxy(IGamePlayer player) {
this.player = player;
}
/** ** Record the time to hit monsters */
private void log(a){
System.out.println("Time to fight monsters:" + new Date().toString());
}
/** * proxy method */
@Override
public void killBoss(a) {
this.log();
player.killBoss();
}
/** * proxy method */
@Override
public void clearance(a) {
player.clearance();
this.count();
}
/** * Calculate the upgrade time */
private void count(a){
System.out.println("It took three days to kill the ghost."); }}Copy the code
Client: Client use case
public class ProxyPattern {
public static void main(String[] args) {
// Service class instance
IGamePlayer player = new GamePlayer("Sun Wukong");
// Proxy class instance
IGamePlayer proxy = new GamePlayerProxy(player);
// method callproxy.killBoss(); proxy.clearance(); }}Copy the code
summary
advantages
Open close principle: New agents can be created without making changes to the server or client
High extensibility: You can control service objects without the client noticing. As long as the service class implements the interface, the proxy class can completely proxy various proxy classes without making any changes.
Intelligent: Proxy classes can be run without compromising the service classes that need to be proxied.
disadvantages
The code can become complex because of the many new classes that need to be created, and the service response can be delayed.
Applicable scenario
- Remote proxy: Provides a local proxy object for an object in a different address space
- Virtual proxy: When creating objects that consume a lot of resources, you can create proxy objects first, delaying the creation of real objects
- Protected proxy: Controls access to an object and can give different permissions to different users
- Caching proxy: Provides temporary storage for the results of a target operation that can be shared by multiple clients
- Synchronous proxy: Several users use an object without conflict
- Smart reference: Provides some additional operations, such as the amount of traffic and number of times a record is accessed