In Dr. Shanhong's "Java and Patterns" book, this describes the proxy mode:
The proxy mode is the structure pattern of the object. The proxy mode provides a proxy object for an object, and the proxy object controls the reference to the original object.
The structure of the proxy mode.
The so-called agent is one person or institution acting on behalf of another person or institution. In some cases, a client does not want or cannot refer directly to an object, whereas a proxy object can act as an intermediary between the client and the target object.
The proxy mode class diagram is as follows:
Roles in proxy mode:
Abstract Object Role (Phone): A common interface between the target object and the proxy object is declared, so that the proxy object can be used wherever the target object can be used.
target Object Role (phonebase): defines the target object that the proxy object represents.
proxy Object Role (PhoneProxy): The proxy object contains a reference to the target object, so that the target object can be manipulated at any time, and the proxy object provides the same interface as the target object so that the target object can be substituted at any time. Proxy objects typically perform an operation before or after a client call is passed to the target object, rather than simply passing the call to the target object.
proxy mode diagram using the Android phone management example, you can see that the reason to use the proxy mode, is to manage the different types of phone, visitors do not need to know what kind of phone the Android system wants, You can use the PhoneProxy object directly. Here's a look at its approximate implementation:
Abstract object Role (Phone):
Public interface Phone { ... .. /** * Get the current servicestate. Use * <code>registerForServiceStateChanged</code> to is informed of * updates. * /ServiceState getservicestate (); /** * Get the current celllocation. * /Celllocation getcelllocation (); /** * @return all available cell information or null if none. * /Public list<cellinfo> getallcellinfo (); ......}
target Object Role (the implementation of Phonebase (Phonebase is embodied in its subclasses, take Gsmphone as an example)):
public class Gsmphone extends Phonebase { ... @Override public servicestate getservicestate () { if (msst! = null) { return MSST.MSS; } else { //Avoid potential NPE in Emergencycallhelper during Phone switch return new servicestate (); } @Override public celllocation getcelllocation () { return msst.getcelllocation (); } @Override public phoneconstants.state getState () { return mct.mstate; } @Override public int Getphonetype () { return phoneconstants.phone_type_gsm; } ......}
proxy Object Role (PhoneProxy):
public class PhoneProxy extends Handler implements phone {... private phone mactivephone; Private Commandsinterface Mcommandsinterface; Private Iccsmsinterfacemanager Miccsmsinterfacemanager; Private Iccphonebookinterfacemanagerproxy Miccphonebookinterfacemanagerproxy; Private Phonesubinfoproxy Mphonesubinfoproxy; Private Icccardproxy Micccardproxy; ... public phoneproxy (Phonebase phone) {... mactivephone = phone;//The specific object is coming in ..... Mcommandsinterface = ((phonebase) mactivephone). MCi; Objects that use specific objects (here is the object of Gsmphone) ...} @Override public servicestate getservicestate () {return mactivephone.getservicestate ();//Call specific object (this is the Gsmphone object ) method to complete the function <pre name= "code" class= "java" style= "LINE-HEIGHT:32PX;" ><span style= "font-family:arial, Helvetica, Sans-serif;" >}</span>
@Override public celllocation getcelllocation () {return mactivephone.getcelllocation (),/** * @return all available cel L information or null if none. */@Override public list<cellinfo> Getallcellinfo () {return mactivephone.getallcellinfo ();}/** * {@inheritDoc} */ @Override public void setcellinfolistrate (int rateinmillis) {mactivephone.setcellinfolistrate (rateinmillis);} ...}
let's see how the client is used:
public class Phonefactory {static private Phone sproxyphone = null; ... public static void Makedefaultphone (context context) {... int phonetype = Telephonymanager . Getphonetype (Networkmode); if (Phonetype = = phoneconstants.phone_type_gsm) {log.i (Log_tag, "Creating gsmphone"); Sproxyphone = new PhoneProxy (new Gsmphone (context, Scommandsinterface, sphonenotifier));//I To make a GSM phone} else if (Phonetype = = PHONECONSTANTS.PHONE_TYPE_CDMA) {switch (Telephonymana Ger.getlteoncdmamodestatic ()) {case PhoneConstants.LTE_ON_CDMA_TRUE:Lo G.I (Log_tag, "Creating cdmaltephone"); Sproxyphone = new PhoneProxy (new Cdmaltephone (context, Scommandsinterface, Sphonenotifier)) ;//I want to create 4G CDMA mobile phone break; Case PhonecOnstants. LTE_ON_CDMA_FALSE:DEFAULT:LOG.I (Log_tag, "Creating cdmaphone"); Sproxyphone = new PhoneProxy (new Cdmaphone (context, Scommandsinte Rface, Sphonenotifier));//I want to create 3G CDMA mobile phone break; } } ......} ......}
Please correct me if there is any wrong place to be continued.
Android and design mode-proxy mode