Object-oriented design pattern (10), Proxy mode

Source: Internet
Author: User
Tags ming

Proxy mode, also known as the delegation mode, is one of the structural design patterns, which is widely used in practical applications, so we must master this design pattern.

Definition: provides a proxy object for an object that controls the behavior of the object through this proxy object.

Usage scenarios:

    • When the behavior of direct access or control objects is difficult, the object's behavior can be indirectly controlled through the object's proxy object, in order to use simple transparency, the delegate object and the proxy object need to implement the same interface, that is, the same type, using the same method.

    • The security of the object can be guaranteed to some extent. That is, instead of exposing the object directly to the client, exposing the object's proxy object to the client makes the object more secure. But this is not the main, or the prototype model is directly used.

and the adapter mode difference:

    • The adapter mode is for an incompatible interface that causes the interface to not be used directly and the adapter is used to fit the interface to reach the intended purpose, and the behavior of the interface may need to be added (extended).

    • Proxy mode in order to make direct control of a class's behavior difficult to use and the class has the same behavior (implement the same interface) proxy object instead of the original object behavior (indirect control), does not add the behavior of the interface (just replace it).

This year, Xiao Ming too little money received, angry, and soon began to school, I do not want to go to the schools report, how to do, Xiao Ming's parents can not let Xiao Ming do not report ah, but also take Xiao Ming no way, that had to be his own instead of Xiao Ming reported Bai, so with Xiao Ming's winter vacation homework to Xiaoming reported ...

Take the example above to implement the code pattern:

Code implementation:

Reported behavior (interface)

/** * 开学报道行为(接口) * @author lt * */publicinterface Report {    // 去学校    void goSchool();    // 交学费    void pay();    // 领书本    void getBook();    // 回家    void goHome();}

Student (the object being represented)

/** * Student (Agent) * @author LT * * * * Public  class Student implements report {    @Override     Public void Goschool() {System.out.println ("happily went to school to report."); }@Override     Public void  Pay() {System.out.println ("paid 4000 tuition!" "); }@Override     Public void GetBook() {System.out.println ("6 books have been collected!" "); }@Override     Public void GoHome() {System.out.println ("Finally the story is finished, go home!" "); }}

Parent (proxy object)

/** * Parent (proxy object) * @author LT * * * * Public  class Parent implements report {    PrivateReport student;//Hold references to proxied objects     Public Parent(Report student) { This. student = student; }@Override     Public void Goschool() {student.goschool (); }@Override     Public void  Pay() {Student.pay (); }@Override     Public void GetBook() {Student.getbook (); }@Override     Public void GoHome() {student.gohome (); }}

test:(a specific Reporting Act)

publicclass Test {    publicstaticvoidmain(String[] args) {        // 小明15岁了,现在在上初中        new Student();        // 小明今年过年没收到去年那么多的压岁钱,不高兴了,去年都是自己去报道,今年不去了        new Parent(children);        parent.goSchool();        parent.pay();        parent.getBook();        parent.goHome();    }}

? Usually the beginning of the school report is very happy, because after all, just finished years, received a lot of lucky money, but also waiting to go to school flowers quickly. But Xiao Ming too much love, too little money, rather die than report, to Xiao Ming himself to the school report too difficult, Xiao Ming's parents had to replace Xiao Ming to register. Instead of Xiao Ming to enroll is not only parents can oh, but also can be their relatives or other and good friends. Of course, Xiao Ming's parents can also replace the children of other families reported, such as relatives or friends of children reported. This shows that when controlling the behavior of an object is difficult, you can use proxy objects instead of having a set of objects that implement the same interface, and an object can have other proxy objects, provided that the two implement the same interface, that is, the same behavior.

Summarize:

The examples of proxy patterns, both in life and in the code world, are common, widely used, have the advantage of solving certain requirements, and have a common problem with design patterns, which is the increase of classes. The key to mastering design patterns is understanding, not rote templates, and when I write code, we can naturally incorporate our design patterns into our programs in certain situations, making our program structure clearer, flexible, easy to scale, and more.

Object-oriented design pattern (10), Proxy mode

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.