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