Although the example Boss->Manager->Employee is already an example of a bad street to use in delegation mode, it is enough to illustrate the classic of this example to explain delegation mode well. This article still uses this example to illustrate delegation mode briefly, but the focus of this article is to clarify delegation mode and delegation mode at the end.The difference between;
Delegation mode: you find me, I act as the intermediary, and I find others according to your needs, which is the delegation mode
The following diagram illustrates the process of delegation mode very well: the diagram was found by a blogger on the Internet.
(Picture address: yinni11 Blog: https://blog.csdn.net/yinni11/article/details/81545889)
1. Employee Interface Staff (both managers and employees implement this interface)
public interface Staff { //The way you do things, pass in the Identity Things to find different implementers void doing(String thing); }
2. Manager Leader
public class Leader implements Staff { //Install different executors to match different executors (value s) according to key private Map<String,Staff> staffMap = new HashMap<String,Staff>(); public Leader(){ //A Employee staffMap.put(ThingEnum.STAFF_A.getThing(),ThingEnum.STAFF_A.getStaff()); //B Employee staffMap.put(ThingEnum.STAFF_B.getThing(),ThingEnum.STAFF_B.getStaff()); } @Override public void doing(String thing) { staffMap.get(thing).doing(thing); } }
3. Employee A:StaffA
public class StaffA implements Staff{ @Override public void doing(String thing) { System.out.println("I am an employee A,I'm good at doing:"+thing); } }
4. Employee B:StaffB
public class StaffB implements Staff{ @Override public void doing(String thing) { System.out.println("I am an employee B,I'm good at doing:"+thing); } }
5. Enumerate ThingEnum class: used to unify what an employee does with what he is good at
public enum ThingEnum { STAFF_A("encryption",new StaffA()), STAFF_B("data processing",new StaffB()); private String thing; private Staff staff; //Private construction methods private ThingEnum(String thing,Staff staff){ this.thing = thing; this.staff = staff; } public String getThing(){return thing;} public Staff getStaff(){return staff;} }
6. Test Class:
public class DelegateTest { public static void main(String[] args) { Leader leader = new Leader(); //I want to encrypt leader.doing("encryption"); //I want to do data processing leader.doing("data processing"); } }
Test results:
Delegation pattern summary: boss (test class), find leader by passing in his own requirements (e.g. encryption), leader finds employee A who is good at data encryption through the needs of the board to operate; it is important to have a relationship in the leader that corresponds to what the employee and the employee are good at (map stores the corresponding relationship)
The difference between delegation mode and agent mode: (Take leaders, managers and employees for example)
1. Agent mode: The leader clearly knows I want to work for Employee A, but I don't look for Employee A directly. I go to the manager and ask the manager to look for Employee A.
Emphasis: The leader clearly knows the existence of employee A (the agent class is passed in when the agent is acting), so the manager will only look for employee A (the manager acting for employee A), similar to: one-to-one relationship;
2. Delegation mode: Leaders don't care which employees they have and don't need to know them. I directly say what I need and go to the manager. Managers look for employees who can do this job according to the needs of leaders (e.g., encrypt to find employee A, data processing to find employee B);
Key point: The leader does not know which employees are there. The leader only cares about who the manager is and what his needs are. Just go to the manager and let him find the employees to complete his work, similar to: one-to-many relationship (employees are multiple)