Design pattern delegation pattern (not one of the 23 classic design patterns)

introduce

Standard definition: The principle of delegation mode is that class B and class A are two classes that have nothing to do with each other. B has exactly the same methods and attributes as A, and calling the methods and attributes in B is calling the methods and attributes of the same name in A. . B seems to be an intermediary authorized by A. The third-party code does not need to know the existence of A, nor does it need to have a direct connection with A. It can directly use the functions of A through B, so that it can not only use various functions of A, but also protect A well. Get up, kill two birds with one stone.
Popular understanding: We are all a project team in project development. After the boss hands over the task to the project manager, the project manager formulates the project plan and assigns the task to the developers below. This is the delegation mode. But we found that this is very similar to the proxy mode we learned before. In fact, this can also be regarded as a special case of the static proxy mode. In addition, the project manager will make a trade-off after receiving the task, how to choose to assign these tasks. We also found that this is very similar to the strategy pattern we learned before. In fact, this can also be regarded as a special case of the strategy pattern. (In fact, design patterns generally do not exist independently, they are all mixed)
What is the difference between the delegation pattern, the static proxy pattern and the strategy pattern?
Delegation model: The agent is solely responsible for the matter. Such as: the boss assigns tasks to the project manager, the project manager is only responsible for scheduling work, and the real work is the developers underneath.
Static agency mode: The agent only participates in a small part of the work of the principal, and the final conclusion must be decided by the agent. For example: Zhang San has no time to find a partner, the matchmaker helps Zhang San find a partner, and finally it depends on whether Zhang Sanxi likes this object or not.
Strategy mode: The project manager needs to make trade-offs when assigning tasks, and there will be a variety of assignment schemes, but in the end, the tasks in their hands are assigned to the developers below.

case

Scenario: The DispatcherServlet in the springmvc framework is actually a typical delegation mode.
Code:
1> Suppose there are the following three actions, each of which performs a different business operation.

public class MemberAction {
    public void getMemberById(String mid){

    }
}
public class OrderAction {
    public void getOrderById(String mid){

    } 
}
public class SystemAction {
    public void logout(String mid){

    }
}

2> The real distribution class

//这个类对客户端发来的请求做分发(类似项目经理分配任务)
public class ServletDispatcher {
    private List<Handler> handlerMapping=new ArrayList<Handler>();
    public ServletDispatcher(List<Handler> handlerMapping) {
        try{
            Class<?> memberActionClass=MemberAction.class;
            handlerMapping.add(new Handler()
                    .setController(memberActionClass.newInstance())
                    .setMethod(memberActionClass.getMethod("getMemberById",new Class[]{String.class}))
                    .setUrl("/web/getMemberById.json"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void doService(HttpServletRequest request, HttpServletResponse response){
        doDispatch(request,response);
    }

    private void doDispatch(HttpServletRequest request, HttpServletResponse response){
        //1.获取用户请求的url
        //如果按照j2ee标准,每个url会对应一个servlet,url由浏览器输入
        //而现在使用委派模式,就只需要配一个url,其他的都交给该类统一管理
        String uri=request.getRequestURI();
        //2.servlet拿到url以后要做权衡(要做判断、要做选择)
        //根据用户请求的url,去找到这个url对应的某一个java类的方法

        //3.通过拿到的url去handlerMapping中找(我们把它认为是策略常量)
        Handler handler=null;
        for(Handler h:handlerMapping){
            if(uri.equals(h.getUrl())){
                handler=h;
                break;
            }
        }
        //4.将具体的任务分发给method(通过反射去调用其对应的方法)
        Object object=null;
        try {
            object=handler.getMethod().invoke(handler.getController(),request.getParameter("mid"));
        }catch (Exception e){
            e.printStackTrace();
        }

        //5.获取到method的执行结果,通过response返回出去
//        response.getWriter().write();
    }

    class Handler{
        private Object controller;
        private Method method;
        private String url;

        public Object getController() {
            return controller;
        }
        public Handler setController(Object controller) {
            this.controller = controller;
            return this;
        }
        public Method getMethod() {
            return method;
        }
        public Handler setMethod(Method method) {
            this.method = method;
            return this;
        }
        public String getUrl() {
            return url;
        }
        public Handler setUrl(String url) {
            this.url = url;
            return this;
        }
    }
}

Summary: The delegation pattern is widely used in spring, but it does not belong to the 23 classic design patterns.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325616492&siteId=291194637