Dota elder brother ordering-static proxy (Java)

Source: Internet
Author: User
Tags dota

Understanding and using the design pattern can cultivate our good Object-Oriented Programming habits. At the same time, in practical applications, we can enjoy the pleasure of having the best experience.

Proxy mode is a useful mode with many variants. The application scenario covers the large structure from the small structure to the entire system. Proxy is the meaning of proxy, we may have the concept of proxy server. The concept of proxy can be interpreted as: there is an intermediate layer between the starting point and the destination, meaning proxy.

The proxy mode is defined to provide a proxy for other objects to control access to this object. In some cases, an object is not suitable or cannot directly reference another object, and the proxy object can play a mediation role between the client and the target object. It can pre-process messages for the delegate object, filter messages and send messages, and perform subsequent processing after the message is executed by the delegate object,


Some may wonder why we don't need a proxy? Let's look at the following scenario:

"I don't know how to hate my country; I beat Dota one day to night; I looked up at the moon and bowed my head to Dota; if my friends and friends in Luoyang asked me, I said I was playing Dota; if I didn't work hard, the boss beat Dota; in the midst of a dying illness, I was shocked. Today, I haven't played Dota yet. being born as a person, I also beat DotA. In my life, I will continue to beat Dota in the next generation... "


This is an overview of boys' dormitories in colleges. Cai is a senior in the Computer Science Department of XX University. The song mentioned above is also a true portrayal of his college career. People call it "little food", and the technology is really a dish at home, but it is "the more frustrated and brave", one day to night to play Dota, do not leave the dormitory for dinner, one phone deal. This is not the case, but again:


Snap a keyboard, "Call, finally win, almost let the other side to do, fortunately the other side of the ghost was dragged away by the girlfriend ...", Looking at the time, "Yeah, it's all 11 o'clock, and it's time to eat again without knowing it." muttered, groping for half a day in the quilt, he copied the "trump card mobile phone" Nokia 1100 and dialed the very familiar phone number...


"Hey, fan, I'm Cai ge .... Well, yes, yes, or the Dota package. Okay, hurry up, let's go !"

Fan ge is also a senior student. He started his "Agent ordering" business since his freshman year. He has already had great "performance" and good reputation. He is also called "Rice brother ".

10 minutes, meal arrives, five minutes after dinner, then the food brother starts his great Dota career again ....

The scene ends.

In the above scenario, Fan ge also sells meals in the canteen. However, Fan ge sells food from the canteen. Xiao Cai entrusts Fan ge to buy food, which is equivalent to buying food in the canteen. This is a typical proxy mode. Let's use the code and diagram:


Source code: sellmeal. Java meal sales Interface

Package COM. bjpowernode. pattern. bookmeal;/*** meal sales interface * @ author longxuan **/public interface sellmeal {/*** meal sales method */Public void sellrice ();}

Restaurant. Java

Package COM. bjpowernode. pattern. bookmeal;/*** restaurant * @ author longxuan **/public class restaurant implements sellmeal {/*** restaurant meal sale */@ overridepublic void sellrice () {system. out. println ("1 serving of rice, 2 serving dishes (cold + hot), 1 serving of egg soup. ");}}

Sellmealproxy. Java ordering Agent Class

Package COM. bjpowernode. pattern. bookmeal;/*** ordering agent class, rice brother * @ author longxuan **/public class implements sellmeal {private sellmeal = NULL; Public sellmealproxy (sellmeal) {This. sellmeal = sellmeal; system. out. println ("Hello, I'm Fan ge from ordering meals:") ;}/ *** meal ordering and sales */@ overridepublic void sellrice () {system. out. println ("this is your Dota package:"); sellmeal. sellrice (); system. out. println ("welcome to your patronage. ");}}

Dotagg. Java client class, ordering brother Dota

Package COM. bjpowernode. pattern. bookmeal;/*** client class, Dota brother * @ author longxuan **/public class dotagg {public static void main (string [] ARGs) {// Fan ge sellmealproxy fangg = new sellmealproxy (new restaurant (); // you can order a meal from fangg. sellrice ();}}

Running result:



From the source code, we can see that the client accesses the target object through a proxy, thus avoiding direct contact with the target object. Therefore, the Dota elder brother can eat the food in the canteen without having to buy his own meals, saving time. Fan ge made a little money, though not much, but he was "small profits", and he made advertisements every time he delivered meals. More customers naturally make more profits, which gives them the value of existence.


That is to say, we do not have to directly allow the original object to access the target object. Sometimes it is not good or inappropriate, or it cannot happen in real life. Therefore, we need a proxy class to help us complete these operations, and these proxies can also pre-process messages and perform subsequent processing. This is the meaning of the existence of the proxy.


If you are more careful, you will find a proxy service and an interface. If there are 100 different target objects, 100 interfaces and corresponding proxy classes are required, which is also the disadvantage of static proxy. How can this problem be solved? Please look forward to my next blog.


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.