Design Mode-in-depth understanding of various proxy modes (2) Popular code version

Source: Internet
Author: User

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;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.