Re-learn the design pattern (3. Design pattern-intermediary pattern)

1. Intermediary model

    For the intermediary, the most common thing people hear is the real estate intermediary. The duty of the real estate intermediary is very simple, that is, to build a bridge between buying and selling a house to communicate the two. This is the intermediary model we are going to learn today.

1.1, what is the intermediary mode

  • definition

    Mediator mode , also known as mediation mode, is a behavioral design mode. The mediator makes each object do not need to display mutual references, but makes program components communicate indirectly through a special mediator object, thereby reducing the coupling between program components. It is a typical application of Dimiter's law.

    This pattern restricts direct communication between objects and forces them to cooperate only through intermediary objects, similar to the Controller part of the MVC pattern, the controller (C) is the intermediary between the model (M) and the view (V).

The structure of the Mediator pattern:

    1) The role of abstract mediator (Mediator): it is the interface of the mediator, which provides an abstract method for registering and forwarding the information of colleague objects.

    2) Concrete Mediator role: implement the mediator interface, define a List to manage colleague objects, and coordinate the interaction between each colleague role, so it depends on the colleague role.

    3) The role of the abstract colleague class (Colleague): define the interface of the colleague class, save the intermediary object, provide the abstract method for the interaction of the colleague object, and realize the public functions of all the colleague classes that interact with each other.

    4) Concrete Colleague role: It is the implementer of the abstract colleague class. When it needs to interact with other colleague objects, the intermediary object is responsible for the subsequent interaction.

    The intent of the mediator pattern is to reduce the chaotic relationship between objects (a large number of many-to-many relationships), manage these objects through a mediator, and encapsulate the interaction between objects in the mediator's object, thereby reducing the coupling between objects.

1.2. Advantages and disadvantages of the intermediary model

  • advantage

    1) In line with the principle of single responsibility, the communication between components is extracted to one place, which reduces the coupling between components and makes it easier to understand and maintain;

    2) It conforms to the principle of opening and closing. When introducing a new intermediary, there is no need to change the original components.

  • shortcoming

    The intermediary turns the original interdependence between multiple objects into a dependency between the intermediary and multiple colleagues. When there are more and more colleagues, the intermediary itself will become complicated and bloated.

1.3. Creation method

    Take Xiao Ming renting a house through an intermediary as an example.

1) Abstract mediator (Mediator) role

//抽象中介者
public interface Mediator {

	void register(String name,HouseOwner owner); //注册同事对象,房主在中介登记了信息
	
	void forwardMessage(HouseOwner owner,int price); //转发同事信息,与同事对象交互信息的方法
}

2) Concrete Mediator role

//具体中介者
public class ConcreteMediator implements Mediator{

	//管理同事对象
	private HashMap<String,HouseOwner> houseowner = new HashMap<String, HouseOwner>();
	
	@Override
	public void register(String name, HouseOwner owner) {
		houseowner.put(name, owner);
	}
	
	@Override
	public void forwardMessage(HouseOwner owner,int price) { 
		if(owner.sendMessage(price)){
			System.out.println("成功租出");
		}else{
			System.out.println("还是看看其他的吧");
		}
	}

}

3) Abstract Colleague role

//抽象同事类
public abstract class HouseOwner {

	private Mediator mediator; //聚合,中介者
	
	public HouseOwner(Mediator mediator){
		this.mediator = mediator;
	}

	public Mediator getMediator() {
		return mediator;
	}
	
	public abstract boolean sendMessage(int price);
}

4) Concrete Colleague roles

//具体同事类,也就是具体的房主A
public class HouseAColleague extends HouseOwner{

	public HouseAColleague(Mediator mediator) {
		super(mediator);
		this.getMediator().register("HouseA", this);
	}

	@Override
	public boolean sendMessage(int price) {
		if(price>=900){
			return true;
		}
		System.out.println("我的房子最少租900块");
		return false;
	}

}

//具体同事类,也就是具体的房主B
public class HouseBColleague extends HouseOwner{

	public HouseBColleague(Mediator mediator) {
		super(mediator);
		this.getMediator().register("HouseB", this);
	}

	@Override
	public boolean sendMessage(int price) {
		if(price>=1500){
			return true;
		}
		System.out.println("我的房子最少租1500块");
		return false;
	}

}

 5) client

public class Client {

	public static void main(String[] args) {
		//创建中介者
		Mediator mediator = new ConcreteMediator();
		
		//中介带顾客看房子A
		HouseOwner houseA = new HouseAColleague(mediator);
		//房东说
		houseA.sendMessage(900);
		//顾客A只愿意出800
		mediator.forwardMessage(houseA,800);
		
		
		//中介带顾客看房子B
		HouseOwner houseB = new HouseBColleague(mediator);
		//房东说
		houseB.sendMessage(1500);
		//顾客A愿意1500租
		mediator.forwardMessage(houseB,1500);
		
	}
}

Case effect:

The UML diagram at this time:

    Customers rent houses through intermediaries, and intermediaries deal with specific landlords.

1.4. Summary and Suggestions

    The main purpose of the intermediary model is to eliminate the interdependence between a group of system components and become dependent on a single intermediary object, which centralizes the communication between system components. The components only know the intermediary objects, and it is difficult to directly communicate with the real components (landlords).

Application scenario:

    1) The intermediary pattern can be used when the dependencies of many objects interacting in complex ways make the system difficult to understand and maintain;

    2) When an object refers to many other objects, which makes it difficult to reuse the object, the intermediary mode can be used.

Application of intermediary mode in JDK:

    java.util.Timer

    java.util.concurrent.Executor#execute()

    java.lang.reflect.Method#invoke()

Guess you like

Origin blog.csdn.net/xiaoxianer321/article/details/125364668