Design patterns learning summary (xx) - intermediary model

definition

Intermediary model is to use a mediation object that encapsulates a set of objects interact intermediary keeping objects explicitly referring to each other, so that it loosely coupled, and can be changed independently of the interaction between them.

Character:

  • Mediator: Mediator abstract. It defines colleagues object to the interface between the intermediary object.

  • ConcreteMediator: specific mediator. Mediator abstract implementation, it needs to know all of the specific class colleagues, also need to receive information from a specific class colleagues there, and send information to a specific class colleagues.

  • Colleague: abstract class colleagues.

  • ConcreteColleague: specific class colleagues. Each specific class colleagues only need to know their behavior, but they need to know intermediaries.

Note: there are a lot of-many links between objects, will lead to a very complex system, these objects will not only affect other objects, it will also be affected by other objects that are called objects colleagues, among them by each other interaction behavior realization of the system.

In intermediaries mode mediator at the core of the object, because it defines the relationship between colleagues throughout the system all the specific class. It is mainly responsible for two areas in the whole system.

  • 1, play the role of the transfer structure. Mediator encapsulated by the object of the relationship, so that no particular class colleagues references to other objects to be displayed, it only needs to complete the communication between the colleagues category through intermediaries.

  • 2, play a collaborative role behavior. Mediator based on the relationship between the encapsulated colleagues, coworkers class does communicate with other objects via intermediaries not need to know in the case of other objects. In this process is necessary to specify class colleagues behave intermediary, the intermediary may be coordinated according to its own logic, a request for further processing colleagues, the relationship between the behavior of the separation members and colleagues encapsulated.

Mediator relationship and because the object is encapsulated, so that the coupling between the respective class colleagues reduced, so that they can be independently changed and reuse.

Advantages and disadvantages

advantage:

  • Simplifying the relationship between objects, the relationship between individual target system encapsulates colleagues respective class of decoupled, so that the system becomes loose coupling.
  • The system provides flexibility so that each co-worker and easy to reuse objects independently.

Disadvantages:

Intermediary model, the intermediary role assumed more responsibility, so once this intermediary objects there is a problem, the whole system will be affected significantly.
When adding a new class colleagues, I had to modify the abstract and concrete classes intermediaries intermediary class, then you can use the observer mode and state mode to solve this problem.

Examples

Rent

Mediator abstract class:

/**
 * 中介者抽象类
 */
public abstract class Mediator {
    public abstract void constact(String message,Person person);
}

Colleagues Object base class:

public class Person {

    protected String name;
    protected Mediator mediator;

    Person(String name,Mediator mediator){
        this.name = name;
        this.mediator = mediator;
    }
}

Colleagues specific categories:

/**
 * 房东
 */
public class HouseOwner extends Person{

    HouseOwner(String name, Mediator mediator) {
        super(name, mediator);
    }

    /**
     * @desc 与中介者联系
     * @param message
     * @return void
     */
    public void constact(String message){
        mediator.constact(message, this);
    }

    /**
     * @desc 获取信息
     * @param message
     * @return void
     */
    public void getMessage(String message){
        System.out.println("房东:" + name +",获得信息:" + message);
    }
}

/**
 * 租客
 */
public class Tenant extends Person{

    Tenant(String name, Mediator mediator) {
        super(name, mediator);
    }

    /**
     * @desc 与中介者联系
     * @param message
     * @return void
     */
    public void constact(String message){
        mediator.constact(message, this);
    }

    /**
     * @desc 获取信息
     * @param message
     * @return void
     */
    public void getMessage(String message){
        System.out.println("租客:" + name +",获得信息:" + message);
    }
}

Specific Agent:

/**
 * 具体中介
 */
public class MediatorStructure extends Mediator {
    // 中介必须知道所有房东和租客的信息
    private HouseOwner houseOwner;
    private Tenant tenant;

    public HouseOwner getHouseOwner() {
        return houseOwner;
    }

    public void setHouseOwner(HouseOwner houseOwner) {
        this.houseOwner = houseOwner;
    }

    public Tenant getTenant() {
        return tenant;
    }

    public void setTenant(Tenant tenant) {
        this.tenant = tenant;
    }

    public void constact(String message, Person person) {
        //如果是房东,则租客获得信息。反之则是房东获得信息
        if (person == houseOwner) {
            tenant.getMessage(message);
        } else {
            houseOwner.getMessage(message);
        }
    }
}

test:

public static void main(String[] args) {
    //一个房东、一个租客、一个中介
    MediatorStructure mediator = new MediatorStructure();
    //房东和租客只需要知道中介即可
    HouseOwner houseOwner = new HouseOwner("张老三", mediator);
    Tenant tenant = new Tenant("李小四", mediator);

    //中介要知道房东和租客
    mediator.setHouseOwner(houseOwner);
    mediator.setTenant(tenant);

    tenant.constact("听说你那里有三室的房子出租.....");
    houseOwner.constact("是的!请问你需要租吗?");
}

Console output:

房东:张老三,获得信息:听说你那里有三室的房子出租.....
租客:李小四,获得信息:是的!请问你需要租吗?

Guess you like

Origin www.cnblogs.com/markLogZhu/p/11582693.html