?
?? Proxy mode Spring Festival home
1 Introduction
Last night, the mother called and asked me that I bought the train ticket. When can I go home, speech is very anxious, which reminds me of the past. I am in Chengdu last year, it is very convenient to go home, including the Spring Festival. Because in Chengdu, the train ticket is very good. Mom never worry about I can't buy tickets, in her eyes, I buy tickets equivalent to the following categories:
Package com.hongsoft.test;
Abstract Public Class Gohome, ABSTRACT PUBLIC
{
?? Abstract public boolean Buyticket ();
}
In the eyes of the mother, the Buyticket () method always returns True, so she has never asked me if I buy tickets during the Spring Festival; how did I actually buy tickets?
2.?? Mechanic purchase
I have been buying tickets in Chengdu, as follows:
Package com.hongsoft.test;
Public Class Realgohome Extends Gohome
{
??? public boolean Buyticket ()
??? {
??????? system.out.println ("pay money, buy ticket");
???}
}
As my mother thinks, the Buyticket () method always returns True, so I can always buy tickets home smoothly; but this year's Spring Festival is not.
3.?? Method for buying this year
I have arrived in Guangzhou this year, the ticket in Guangzhou Spring Festival can be difficult to buy! I call my Buyticket () method is much better, but I will return false every time, there is no ticket! What should I do? I don't go back, I can't, helpless, I I thought of the train ticket agent.
I know that the agent during the Spring Festival is such a ticket:
Package com.hongsoft.test;
Public Class Proxygohome Extends Gohome
{
??? private realgohome realgohome
??? // Implement the purchase method
??? public boolean Buyticket ()
??? {
?????? prebuy ();
?????? if (Realgohome == null)
?????????? realgohome = new realgohome ();
?????? realgohome. Buyticket ();
?????? postbuy (); ??????
???}
??? // Action before buying a ticket
?????prate void prebuy ()
??? {
?????? system.out.println ("call to the relational people, let the relational people gave a speech to spend");
???}
??? // The action after buying tickets
???priVate void postbuy ()
??? {
?????? system.out.println ("Refentee Mixi Mixi");
???}
}
Summary
In this way, through the train ticket agent, I got a ticket smoothly today! Experience summary as follows:
1) ????? I bought tickets through the train ticket agent as follows:
Gohome gohome = new proxygohome ();
GoHome.buyticket ();
But I declare that the code is only used to learn the Proxy mode, can't compile!
2) ???? The above is the application of the Proxy mode, and GoHome is an abstract body. Realgohome is a real body, and Proxygohome is a proxy main body.
3) ???? Should be applied in a suitable place:
If you usually buy a ticket, use Proxy mode, not necessarily, because I have to spend my agency fee!
But when you arrive in the Spring Festival in Guangzhou, you still don't have to use proxy mode, ........., huh, unless you are not preparing to go home in the Spring Festival!
4) ???? In this example, Proxygohome has prebuy () and postbuy () methods, in this case, no tickets are not available; in the actual programming environment, pre-processing and post-processing methods are generally used Implement specific functions: such as managing database connections, managing network connections, access control, implementing Cache, EJB client pretreatment, etc. Because a proxy class is a lot of customer service, we generally realize abstract features from the agent in the pre-processing and post-processing methods.
5) Be careful, proxy is serving customer service, such as this example, the agent is serving me; the train station does not need Proxy as a train station service, this is the most essential difference between Proxy mode and FACADE mode, later This article will analyze this in detail. ?
Ok, let's write here, I want to call my mom, let her rest assured!
?
HONGBO781202, also known as hongsoft, professional programmer, research area: 1) Based on workflow 2) Java-based information security
Technology. Welcome to discuss all aspects of Java related aspects of technology and market issues hongbosoftware@163.com
?????