Design Pattern (3) Proxy pattern

Source: Internet
Author: User

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

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.