Delegation mode is a behavioral mode. The principle of this mode is that class B and class A are two classes that have no relationship with each other. Class B has the same methods and properties as class A; and calling methods and properties in class B is calling methods and properties with the same name in class A. B seems to be an intermediary entrusted by A. The third-party code does not need to know the existence of a, nor need to have a direct contact with A. through B, you can directly use a's functions, so that you can use a's various functions, and protect a well, and achieve two things with one stone. Here are two code demonstrations:
The first is the dispatcher Servlet of the Servlet:
package pattern.delegate; import pattern.delegate.controllers.MemberAction; import pattern.template.entity.Member; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class ServletDispatcher { private List<Handler> handlerMapping=new ArrayList<>(); public ServletDispatcher(){ try { Class<?> clazz= Member.class; handlerMapping.add(new Handler().setController(clazz.newInstance()).setMethod(clazz.getMethod("getMemberById",new Class[]{String.class})).setUrl("/web/getMemberById.json")); }catch (Exception e){ e.printStackTrace(); } } public void doService(HttpServletRequest request, HttpServletResponse response){ try { doDispatch(request,response); } catch (Exception e) { e.printStackTrace(); } } private void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { String uri=request.getRequestURI(); Handler handle=null; for (Handler h:handlerMapping){ if(uri.equals(h.getUrl())){ handle=h; break; } } Object object=handle.getMethod().invoke(handle.getController(),request.getParameter("id")); response.getWriter().append(""); } 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; } } }
package pattern.delegate.controllers; public class MemberAction { public void getMemberById(String id){ System.out.println("Query member number:"+id); } }
package pattern.delegate.controllers; public class OrderAction { public void getOrderById(String id){ System.out.println("Query order number:"+id); } }
package pattern.delegate.controllers; public class SystemAction { public void logout(){ System.out.println("Sign out"); } }
It is mainly to understand what is delegation mode.
Here is an example of a manager sending tasks to employees:
package pattern.delegate.leader; public class Boss { public static void main(String[] args) { //Customer request(Boss),Delegated by(Leader),Delegate(Target) //Delegate to hold references to delegates //Agent mode is the active process,The delegator's initiative is the result //The strategic model focuses on scalability(External expansion),Delegation model focuses on internal flexibility and reuse //Delegation mode is a combination of static agent and policy mode new Leader().doing("Sign in"); } }
package pattern.delegate.leader; public interface ITarget { public void doing(String command); }
package pattern.delegate.leader; import java.util.HashMap; import java.util.Map; public class Leader implements ITarget { private Map<String,ITarget> targetMap=new HashMap<>(); public Leader() { targetMap.put("encryption",new TargetA()); targetMap.put("Sign in",new TargetA()); } /** * The project manager does not work, but only assigns tasks * @param command */ public void doing(String command){ targetMap.get(command).doing(command); } }
package pattern.delegate.leader; public class TargetA implements ITarget { @Override public void doing(String command) { System.out.println("I'm an employee A,I'll start now:"+command); } }
package pattern.delegate.leader; public class TargetB implements ITarget { @Override public void doing(String command) { System.out.println("I'm an employee B,I'll start now:"+command); } }
This example is easier to understand than the above one. It needs good taste.