1. Mandatory Agent
(1) Forcing a proxy is the caller calling the real role directly, without worrying about whether the proxy class exists. The creation of its agent is determined by the real role.
(2) Through the real role, to find the role of the agent, and then through the role of proxy, the method of tuning the real role
2. Code
(1) Abstract class, more than one GetProxy method
Package com.design. Proxy mode. mandatory agent; Public Interface Igameplayer { void Login (string username, string pwd); void Killboss (); void upgrade (); Igameplayer getproxy ();}
View Code
(2) Real characters
Packagecom.design. Proxy mode. mandatory agent; Public classGameplayerImplementsIgameplayer {PrivateString name = ""; PrivateIgameplayer proxy =NULL; PublicGameplayer (String name) {Super(); This. Name =name; } @Override PublicIgameplayer GetProxy () { This. Proxy =NewGameplayerproxy ( This); return This. Proxy; } @Override Public voidLogin (string username, string pwd) {if( This. IsProxy ()) {System.out.println ("Name:" + name + "Login ..."); }Else{System.out.println ("Please use a proxy"); }} @Override Public voidKillboss () {if( This. IsProxy ()) {System.out.println ("Name:" + name + "Killboss ..."); }Else{System.out.println ("Please use a proxy"); }} @Override Public voidupgrade () {if( This. IsProxy ()) {System.out.println ("Name:" + name + "Upgrade ..."); }Else{System.out.println ("Please use a proxy"); } } Private BooleanIsProxy () {if( This. Proxy = =NULL){ return false; }Else{ return true; } }}
View Code
(3) proxy class
Packagecom.design. Proxy mode. mandatory agent;Importjava.util.Date; Public classGameplayerproxyImplementsIgameplayer {PrivateIgameplayer Gameplayer =NULL; Publicgameplayerproxy (Igameplayer gameplayer) {Super(); This. Gameplayer =Gameplayer; } @Override Public voidLogin (string username, string pwd) {System.out.println (username+ "Start Login"); This. Gameplayer.login (username, pwd); SYSTEM.OUT.PRINTLN (username+ "Login Complete"); } @Override Public voidKillboss () {System.out.println ("Start playing strange" +NewDate ()); This. Gameplayer.killboss (); Thread.CurrentThread (); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("End of the fight" +NewDate ()); } @Override Public voidupgrade () { This. Gameplayer.upgrade (); System.out.println ("Congratulations on the completion of the upgrade"); } @Override PublicIgameplayer GetProxy () {return This; }}
View Code
(4) Scene class
Packagecom.design. Proxy mode. mandatory agent; Public classClient { Public Static voidMain (string[] args) {//1. Do not use the agent, see the results, only print please use the agentIgameplayer GP1 =NewGameplayer ("Lvyf"); Gp1.login ("Lvyf", "123"); Gp1.killboss (); Gp1.upgrade (); //2. Use the proxy class or notIgameplayer GP2 =NewGameplayer ("Lvyf"); Igameplayer GP3=NewGameplayerproxy (GP2); Gp3.login ("Lvyf", "123"); Gp3.killboss (); Gp3.upgrade (); //3. Find the proxy role through the real role, and then through the proxy role, the way to adjust the real roleIgameplayer GP4 =NewGameplayer ("Lvyf"); Igameplayer Proxy=Gp4.getproxy (); Proxy.login ("Lvyf", "123"); Proxy.killboss (); Proxy.upgrade (); }}
Design mode--5.3 Proxy mode-Mandatory proxy