Design mode--5.2 Proxy mode--general agent

Source: Internet
Author: User

1. The general agent is that we need to know the presence of the proxy class (Subject) before we can access

(1) The caller knows only that the agent exists, so you don't have to know who the agent is.
(2) Shielding the effects of changes in real-world characters (Realsubject) on high-level classes (scene classes)

(3) The real theme of the role of how to modify how to modify, the high-level module has no impact, as long as you implement the interface corresponding to the method, the model is very suitable for the requirements of high scalability occasions.

2.

(1) Interface class

 Package com.design. Proxy mode. First stage;  Public Interface Igameplayer {    void  Login (string username, string pwd);         void Killboss ();         void upgrade ();}    
View Code

(2) Real characters

 Packagecom.design. Proxy mode. General agent;Importcom.design. Proxy mode. First stage. Igameplayer; Public classGameplayerImplementsIgameplayer {PrivateString name = "";  PublicGameplayer (Igameplayer _gameplayer, String _name)throwsexception{//only agents can create objects        if(_gameplayer! =NULL&& _gameplayer.getclass (). GetName (). Equals ("Com.design. Proxy mode. General agent. Gameplayerproxy")){             This. Name =_name; }Else{            Throw NewException ("Cannot create a real role"); }    }    //Login@Override Public voidLogin (string username, string pwd) {System.out.println ("Gameplayer:" + name + "Login ..."); }    //Kill the Monsters .@Override Public voidKillboss () {System.out.println ("Gameplayer:" + name + "Killboss ..."); }    //Upgrade@Override Public voidupgrade () {System.out.println ("Gameplayer:" + name + "Upgrade ..."); }}
View Code

(3) Proxy

 Packagecom.design. Proxy mode. General agent;Importcom.design. Proxy mode. First stage. Igameplayer; Public classGameplayerproxyImplementsIgameplayer {PrivateIgameplayer Gameplayer =NULL;  PublicGameplayerproxy (String name)throwsexception{Gameplayer=NewGameplayer ( This, name); }        //determine who you want to leveling through the constructor function     Publicgameplayerproxy (Igameplayer _gameplayer) {Super();  This. Gameplayer =_gameplayer; }    //Leveling Login@Override Public voidLogin (string username, string pwd) { This. Gameplayer.login (username, pwd); }    //leveling Kill a monster@Override Public voidKillboss () { This. Gameplayer.killboss (); }    //Leveling Upgrade@Override Public voidupgrade () { This. Gameplayer.upgrade (); }}
View Code

(4) Scene class

 Packagecom.design. Proxy mode. General agent;Importcom.design. Proxy mode. First stage. Igameplayer; Public classClient { Public Static voidMain (string[] args)throwsException {//The caller knows only that the proxy exists, and you don't have to know who the agent is.//masks The effects of changes in real-world characters (Realsubject) on scene classes        /** The real theme of the role of how to modify the change, the high-level module has no impact, as long as you implement the interface corresponding to the method, the model is very suitable for the requirements of high scalability occasions.         Of course, in a real project, it is generally a good plan to prohibit new from being a real role through conventions. */Igameplayer Proxy=NewGameplayerproxy ("Lvyf"); Proxy.login ("Lvyafei", "123456");        Proxy.killboss ();    Proxy.upgrade (); }}

Design mode--5.2 Proxy mode--general agent

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.