(1) AbstractMediator: Abstract mediator role, which defines the interface from colleague object to mediator object, and is generally implemented in the way of abstract class.
(2) Mediator: specific mediator role, inherited from the abstract mediator, implements the method defined by the parent class. It receives messages from specific colleague objects and issues commands to specific colleague objects.
(3) AbstractColleague: Abstract colleague class role, which defines the interface of the mediator object. It only knows the mediator and does not know other colleague objects.
(4) ColleagueA, ColleagueB: the role of specific colleague class inherits from the abstract colleague class. Each specific colleague class knows its behavior in a small range, but does not know its purpose in a large range.
Understanding + association:
It can be imagined that there are two people in the company who need to complete their own tasks, some of which can only be completed through cooperation
Code analysis:
/** * Created by kim on 2018/9/7. * Abstract colleague class */ public abstract class AbstractColleague { protected AbstractMediator mediator; public AbstractColleague(AbstractMediator mediator){ this.mediator=mediator; } public void setMediator(AbstractMediator mediator){ this.mediator=mediator; } }
/** * Created by kim on 2018/9/7. * tertium quid */ abstract class AbstractMediator { protected HashMap<String,AbstractColleague> colleagues=new HashMap<>(); public void addColleague(String name,AbstractColleague c){ this.colleagues.put(name,c); } public void deleteColleague(String name){ this.colleagues.remove(name); } public abstract void execute(String name,String method); }
public class ColleagueA extends AbstractColleague { public ColleagueA(AbstractMediator mediator) { super(mediator); } public void self(){ System.out.println("Do your part"); } public void out(){ System.out.println("Colleague A--> Asking colleagues B Do your job well"); super.mediator.execute("ColleagueB","self"); } }
public class ColleagueB extends AbstractColleague{ public ColleagueB(AbstractMediator mediator) { super(mediator); } public void self(){ System.out.println("Colleague B Do your part"); } public void out(){ System.out.println("Colleague B --> Asking colleagues A Do a good job of internal work ..."); super.mediator.execute("ColleagueA", "self"); } }
/** * Created by kim on 2018/9/7. */ public class Mediator extends AbstractMediator { @Override public void execute(String name, String method) { //Do your own job if("self".equals(method)){ if("ColleagueA".equals(name)) { ColleagueA colleague = (ColleagueA)super.colleagues.get("ColleagueA"); colleague.self(); }else { ColleagueB colleague = (ColleagueB)super.colleagues.get("ColleagueB"); colleague.self(); } }else{//Working with other colleagues if("ColleagueA".equals(name)) { ColleagueA colleague = (ColleagueA)super.colleagues.get("ColleagueA"); colleague.out(); }else { ColleagueB colleague = (ColleagueB)super.colleagues.get("ColleagueB"); colleague.out(); } } } }