Design Mode-in-depth understanding of various proxy modes (1) Popular code version
1. General proxy mode (beneficial for extension development), which encapsulates real roles for proxies
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 class GamePlayer implements IGamePlayer {
Private String name = "";
// Constructor limits who can create objects and pass names at the same time
Public GamePlayer (IGamePlayer _ gamePlayer, String _ name) throws Exception {
If (_ gamePlayer = null ){
Throw new Exception ("cannot create a real role! ");
} Else {
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 {
Private IGamePlayer gamePlayer = null;
// Pass the constructor to the target user and encapsulate the agent.
Public GamePlayerProxy (String name ){
Try {
GamePlayer = new GamePlayer (this, name );
} Catch (Exception e ){
// TODO Exception Handling
}
}
// 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 ();
}
}
-------
Public class Client {
Public static void main (String [] args ){
// Define another trainer
IGamePlayer proxy = new GamePlayerProxy ("Zhang San ");
// 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 ");
}
}
2. Forced proxy mode: Find the specified proxy for the real role
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 ();
// You can find your proxy.
Public IGamePlayer getProxy ();
}
----
Public class GamePlayer implements IGamePlayer {
Private String name = "";
// Who is my proxy?
Private IGamePlayer proxy = null;
Public GamePlayer (String _ name ){
This. name = _ name;
}
// Find your proxy
Public IGamePlayer getProxy (){
This. proxy = new GamePlayerProxy (this );
Return this. proxy;
}
// Kill monsters. The most expected one is kill old monsters.
Public void killBoss (){
If (this. isProxy ()){
System. out. println (this. name +! ");
} Else {
System. out. println ("Please use the specified proxy to access ");
}
}
// You must log on before entering the game. This is a necessary condition.
Public void login (String user, String password ){
If (this. isProxy ()){
System. out. println ("user with login name" + user + "" + this. name + "successfully logged on! ");
} Else {
System. out. println ("Please use the specified proxy to access ");;
}
}
// There are many methods for upgrading. You can pay for it and perform tasks.
Public void upgrade (){
If (this. isProxy ()){
System. out. println (this. name + "again! ");
} Else {
System. out. println ("Please use the specified proxy to access ");
}
}
// Check whether it is a proxy access
Private boolean isProxy (){
If (this. proxy = null ){
Return false;
} Else {
Return true;
}
}
}
----
Public class GamePlayerProxy implements IGamePlayer {
Private IGamePlayer gamePlayer = null;
// User name passed by the constructor
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 ();
}
// The proxy does not exist yet. It is the proxy.
Public IGamePlayer getProxy (){
Return this;
}
}
----
Public class Client {
Public static void main (String [] args ){
IGamePlayer proxy = null;
// Define a game role
IGamePlayer player = new GamePlayer ("James ");
// Real role
// Proxy = player;
// Create a new proxy
// Proxy = new GamePlayerProxy (player );
// Obtain the specified proxy (the proxy specified by the proxy itself)
Proxy = player. getProxy (); // the real role finds the specified proxy
System. out. println (">>>" + proxy );
// System. out. println (proxy );
// 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 ");
}
}