Design Mode-in-depth understanding of various proxy modes (2) Popular code version
3. Multi-proxy mode: improve or enhance other proxy services, such as interception and filtering.
Public interface IGamePlayer {
// Log on to the game
Public void login (String user, String password );
// Kill monsters, which are the main features of online games
Public void killBoss ();
// Upgrade
Public void upgrade ();
}
---
Public interface IProxy {
// Calculate the fee
Public void count ();
}
---
Public class GamePlayer implements IGamePlayer {
Private String name = "";
// Pass the name through the constructor
Public GamePlayer (String _ name ){
This. name = _ name;
}
// Kill monsters. The most expected one is kill old monsters.
Public void killBoss (){
System. out. println (this. name +! ");
}
// You must log on before entering the game. This is a necessary condition.
Public void login (String user, String password ){
System. out. println ("user with login name" + user + "" + this. name + "successfully logged on! ");
}
// There are many methods for upgrading. You can pay for it and perform tasks.
Public void upgrade (){
System. out. println (this. name + "again! ");
}
}
---
Public class GamePlayerProxy implements IGamePlayer, IProxy{
Private IGamePlayer gamePlayer = null;
// Pass the constructor to whom to train
Public GamePlayerProxy (IGamePlayer _ gamePlayer ){
This. gamePlayer = _ gamePlayer;
}
// Train and kill monsters
Public void killBoss (){
This. gamePlayer. killBoss ();
}
// Logon
Public void login (String user, String password ){
This. gamePlayer. login (user, password );
}
// Training agent upgrade
Public void upgrade (){
This. gamePlayer. upgrade ();
This. count ();
}
// Calculate the fee
Public void count (){
System. out. println ("total upgrade fee: 150 RMB ");
}
}
---
Public class Client {
Public static void main (String [] args ){
// Define an obsessed player
IGamePlayer player = new GamePlayer ("James ");
// Define another trainer
IGamePlayer proxy = new GamePlayerProxy (player );
// Start playing the game and write down the timestamp
System. out. println ("Start Time ");
Proxy. login ("zhangSan", "password ");
// Start killing monsters
Proxy. killBoss ();
// Upgrade
Proxy. upgrade ();
// Record the game end time
System. out. println ("End Time ");
}
}
4. Virtual Proxy: before each method, determine whether the real topic (subject to proxy object) is initialized. If the new method is not initialized, it is actually a proxy object.
For example:
Public class Client {
Public static void main (String [] args ){
// Define an obsessed player
IGamePlayer player = null;
// Define another trainer
IGamePlayer proxy = new GamePlayerProxy (player );// The player must be initialized in the GamePlayerProxy constructor class.
// Start playing the game and write down the timestamp
System. out. println ("Start Time ");
Proxy. login ("zhangSan", "password ");
// Start killing monsters
Proxy. killBoss ();
// Upgrade
Proxy. upgrade ();
// Record the game end time
System. out. println ("End Time ");
}
}
----------------------------------
// Pass the constructor to whom to train
Public GamePlayerProxy (IGamePlayer _ gamePlayer ){
If (_ gamePlayer = null ){
_ GamePlayer = new GamePlayer ("James ");
}
This. gamePlayer = _ gamePlayer;
}