Today I learned the Proxy mode. The proxy mode is used to access an existing object in proxy mode when it is difficult or inconvenient to operate on an existing object. The Proxy implementation method must be consistent with the Proxy object. For example, there is a Math class that implements the IMath interface package com. proxy; public interface IMath {public int add (int a, int B); public int sub (int a, int B); public int mul (int a, int B ); public int dev (int a, int B);} package com. proxy; public class Math implements IMath {@ Overridepublic int add (int a, int B) {return a + B ;}@ Overridepublic int sub (int a, int B) {return a-B ;}@ Overridepublic int mul (int a, int B) {return a * B ;}@ Overridepublic int dev (int a, int B) {return a/B ;}} package com. proxy; import static org. junit. assert. *; import org. junit. test; public class Client {@ Testpublic void test () {Math math = new Math (); int a = 2; int B = 1; assertEquals (3, math. add (a, B); assertEquals (1, math. sub (a, B); assertEquals (2, math. mul (a, B); assertEquals (2, math. dev (a, B) ;}} if there is a proxy class ProxyMath proxy Math class. Write the test package com. proxy; import static org. junit. assert. *; import org. junit. test; public class Client {@ Testpublic void test () {ProxyMath proxy = new ProxyMath (); int a = 2; int B = 1; assertEquals (3, proxy. add (a, B); assertEquals (1, proxy. sub (a, B); assertEquals (2, proxy. mul (a, B); assertEquals (2, proxy. dev (a, B) ;}} the ProxyMath class inherits from the IMath interface package com. proxy; public class ProxyMath implements IMath {private Math math; public ProxyMath () {math = new Math () ;}@ Overridepublic int add (int a, int B) {return math. add (a, B) ;}@ Overridepublic int sub (int a, int B) {return math. sub (a, B );}
@ Overridepublic int mul (int a, int B) {return math. mul (a, B) ;}@ Overridepublic int dev (int a, int B) {return math. dev (a, B );}