Android and design mode-Proxy mode, androidproxy

Source: Internet
Author: User

Android and design mode-Proxy mode, androidproxy

The Proxy mode is described as follows in Dr. Yan Hong's book JAVA and patterns:

  The proxy mode is the structure mode of the object. The proxy mode provides a proxy object for an object and controls the reference to the original object by the proxy object.

Proxy mode structure.

A proxy means that one person or institution takes action on behalf of another person or institution. In some cases, a client does not want or cannot directly reference an object, and the proxy object can play a mediation role between the client and the target object.

The proxy mode class diagram is as follows:

Role in proxy mode:

Abstract object role (Phone ):Declares the common interfaces of the target object and the proxy object, so that the proxy object can be used wherever the target object can be used.

Target object role (PhoneBase ):Defines the target object represented by the proxy object.

Proxy object role (PhoneProxy ):The proxy object contains references of the target object, so that the target object can be operated at any time. The proxy object provides the same interface as the target object, so that the target object can be replaced at any time. A proxy Object usually performs an operation before or after the client call is passed to the target object, instead of simply passing the call to the target object.

The proxy mode graph uses the Android Phone management example. You can see that the agent mode is used to manage different types of phones, visitors can directly use the PhoneProxy object without knowing what type of Phone the Android system wants. Let's take a look at its implementation:

Abstract object role (Phone ):

public interface Phone {    .....    /**     * Get the current ServiceState. Use     * <code>registerForServiceStateChanged</code> to be 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 (PhoneBase (the specific implementation of PhoneBase is embodied in its subclass, taking 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 comment mIccSmsInterfaceManager; private Comment comment; private PhoneSubInfoProxy mPhoneSubInfoProxy; private IccCardProxy mIccCardProxy ;...... public PhoneProxy (PhoneBase phone ){...... mActivePhone = phone; // The object is passed in ...... mCommandsInterface = (PhoneBase) mActivePhone ). mCi; // use the object of a specific object (here is the object of GSMPhone .......} @ Override public ServiceState getServiceState () {return mActivePhone. getServiceState (); // call a specific object (here is the object of GSMPhone) 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 cell 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 want to create a GSM mobile phone} else if (phoneType = PhoneConstants. PHONE_TYPE_CDMA) {switch (TelephonyManager. getLteOnCdmaModeStatic () {case PhoneConstants. lte_on_cdma _true: Log. I (LOG_TAG, "Creating CDMALTEPhone"); sProxyPhone = new PhoneProxy (new CDMALTEPhone (context, sCommandsInterface, sPhoneNotifier); // I want to create 4G CDMA phone break; case PhoneConstants. lte_on_cdma _false: default: Log. I (LOG_TAG, "Creating CDMAPhone"); sProxyPhone = new PhoneProxy (new CDMAPhone (context, sCommandsInterface, sPhoneNotifier )); // I want to create a 3G CDMA mobile phone break ;}}......}......}

Not yet resumed. please correct me if there is anything wrong.

Related Article

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.