"Proxy Mode" of Design Patterns

What is proxy mode

The proxy pattern is a structural design pattern that allows access to the original object to be controlled by adding a proxy without changing the original code. In short, the proxy mode is to hand over the operation of one object to another object. This process hides the real object, so that the client only needs to interact with the proxy object.

Why use proxy mode

In actual development, we often need to restrict or enhance an object, but we don't want to modify or expand this object. At this time, using the proxy mode can solve this problem well. Through the proxy object, we can hide the implementation details of the real object, and at the same time control the real object.

Where do you use it at work

In Android development, proxy mode is also widely used. For example, we can use proxy mode to control access to an Activity to control Activity permissions; we can also use proxy mode to encapsulate network requests to control request traffic and request frequency; at the same time, proxy mode can also be used to implement Event callback mechanism.

Design ideas

The design idea of ​​the proxy mode is relatively simple, we only need to add a proxy on the basis of the original object. The proxy object holds a reference to the real object and also implements the same interface as the real object. When the client needs to access the real object, it first accesses through the proxy object, and the proxy object forwards the request to the real object.

Code

Let's demonstrate the implementation of the proxy mode through a specific example. In this example, we will use the proxy mode to implement the control of Activity permissions.
First, we define an Activity interface to define the basic behavior of the Activity:

public interface Activity {
    
    
    void onCreate(Bundle savedInstanceState);
    void onStart();
    void onResume();
    void onPause();
    void onStop();
    void onDestroy();
}

Then, we define a real Activity that implements all the methods defined in the Activity interface:

public class RealActivity implements Activity {
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
    
        Log.i("RealActivity", "onCreate");
    }

    @Override
    public void onStart() {
    
    
        Log.i("RealActivity", "onStart");
    }

    @Override
    public void onResume() {
    
    
        Log.i("RealActivity", "onResume");
    }

    @Override
    public void onPause() {
    
    
        Log.i("RealActivity", "onPause");
    }

    @Override
    public void onStop() {
    
    
        Log.i("RealActivity", "onStop");
    }

    @Override
    public void onDestroy() {
    
    
        Log.i("RealActivity", "onDestroy");
    }
}

Next, we define a proxy object that holds a reference to the real Activity. In the proxy object, we can control the permissions of the real Activity to control access to the Activity:

public class ProxyActivity implements Activity {
    
    
    private RealActivity realActivity;

    public ProxyActivity(RealActivity realActivity) {
    
    
        this.realActivity = realActivity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    
    
        if (checkPermission("create")) {
    
    
            realActivity.onCreate(savedInstanceState);
        }
    }

    @Override
    public void onStart() {
    
    
        if (checkPermission("start")) {
    
    
            realActivity.onStart();
        }
    }

    @Override
    public void onResume() {
    
    
        if (checkPermission("resume")) {
    
    
            realActivity.onResume();
        }
    }

    @Override
    public void onPause() {
    
    
        if (checkPermission("pause")) {
    
    
            realActivity.onPause();
        }
    }

    @Override
    public void onStop() {
    
    
        if (checkPermission("stop")) {
    
    
            realActivity.onStop();
        }
    }

    @Override
    public void onDestroy() {
    
    
        if (checkPermission("destroy")) {
    
    
            realActivity.onDestroy();
        }
    }

    private boolean checkPermission(String permission) {
    
    
        // 这里可以进行权限控制,比如根据用户角色判断是否有权限访问
        return true;
    }
}

Finally, we can access the real Activity through the proxy object:

Activity activity = new ProxyActivity(new RealActivity());
activity.onCreate(null);  // 输出 "onCreate"
activity.onStart();       // 输出 "onStart"
activity.onResume();      // 输出 "onResume"
activity.onPause();       // 输出 "onPause"
activity.onStop();        // 输出 "onStop"
activity.onDestroy();     // 输出 "onDestroy"

Summarize

The proxy pattern is a very common design pattern that allows access to an object to be controlled without modifying the original code. In Android development, the proxy mode is also widely used, such as implementing Activity permission control, encapsulating network requests, and implementing event callbacks. Mastering the proxy mode allows us to better design and organize code, and improve the maintainability and scalability of the system.

Guess you like

Origin blog.csdn.net/weixin_45112340/article/details/129946198