Mediator Design Pattern

Scenes:

If there is no general manager, the following three departments: the finance department, the marketing department, and the research and development department. The Finance Department needs to pay wages, so that everyone can check that the company needs to communicate with the Marketing Department and the R&D Department; the Marketing Department needs to take on a new project, which requires the R&D Department to handle the technology and the Finance Department to provide funds. The Marketing Department deals with various departments. Although the three departments, the relationship is very chaotic.

In fact, the company has a general manager, and every department will notify the general manager of anything, and the general manager is informing all relevant departments.


Nature:

Decouple interactions between multiple colleague objects. Each object holds a reference to the mediator object and only deals with the mediator object.


scenes to be used:

1.mvc mode (where c, the controller is a mediator object, both m and v deal with him)

2. Window game program, window object is also a mediator object in window software development.

3. Graphical interface development In GUI, the interaction between multiple components can be solved by introducing a finalizer object, which can be the entire window object or DOM object.

4.java.lang.reflect.Method#invoke()


Class Diagram:



package com.gcxzflgl.mediator;

public interface Mediator {
	
	void register(String dname,Department d);
	
	void command(String dname);
	
}
package com.gcxzflgl.mediator;

//Interface of the colleague class
public interface Department {
	void selfAction(); //Do the things of this department
	void outAction(); //Send an application to the general manager
}
package com.gcxzflgl.mediator;

import java.util.HashMap;
import java.util.Map;

public class President implements Mediator {
	
	private Map<String,Department> map = new HashMap<String , Department>();
	
	@Override
	public void command(String dname) {
		map.get(dname).selfAction();
	}

	@Override
	public void register(String dname, Department d) {
		map.put(dname, d);
	}

}
package com.gcxzflgl.mediator;

public class Development implements Department {

	private Mediator m; //Hold a reference to the mediator (general manager)
	
	public Development(Mediator m) {
		super();
		this.m = m;
		m.register("development", this);
	}

	@Override
	public void outAction() {
		System.out.println("Report work! No money, need financial support!");
	}

	@Override
	public void selfAction() {
		System.out.println("Concentrate on scientific research, develop projects!");
	}

}
package com.gcxzflgl.mediator;

public class Finacial implements Department {

	private Mediator m; //Hold a reference to the mediator (general manager)
	
	public Finacial(Mediator m) {
		super();
		this.m = m;
		m.register("finacial", this);
	}

	@Override
	public void outAction() {
		System.out.println("Report work! No money, too much money! How to spend?");
	}

	@Override
	public void selfAction() {
		System.out.println("Count the money!");
	}

}
package com.gcxzflgl.mediator;

public class Market implements Department {

	private Mediator m; //Hold a reference to the mediator (general manager)
	
	public Market(Mediator m) {
		super();
		this.m = m;
		m.register("market", this);
	}

	@Override
	public void outAction() {
		System.out.println("Report work! The progress of the project, needs financial support!");
		
		m.command("finacial");
		
	}

	@Override
	public void selfAction() {
		System.out.println("Run to pick up the project!");
	}

}

test:

package com.gcxzflgl.mediator;

public class Client {
	public static void main(String[] args) {
		Mediator m = new President();
		
		Market   market = new Market(m);
		Development devp = new Development(m);
		Finacial f = new Finacial(m);
		
		market.selfAction();
		market.outAction();
		
	}
}






Guess you like

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