Mediator mode (Mediator)

1. Definitions

Use an intermediary object to encapsulate a series of object interactions. Mediators decouple objects from needing to refer to each other explicitly, making them loosely coupled and can independently alter the interactions between them.

2. Structure

write picture description here

  • Mediator: The mediator defines an interface for communicating with Colleague objects.
  • ConcreteMediator: The concrete mediator realizes cooperative behavior by coordinating various colleague objects, and understands and maintains its various colleagues.
  • Colleague: abstract colleague class.
  • ConcreteColleague: Concrete colleague class. Each concrete class of colleagues only needs to know their own behavior, but they all need to know the mediator.

3. Examples

Let's use an example to illustrate what a colleague class is: there are two classes A and B, each with a number, and it is guaranteed that the number in class B is always 100 times the number in class A. That is, when modifying the number of class A, multiply this number by 100 and assign it to class B, and when modifying class B, divide the number by 100 and assign it to class A. Class A and Class B interact with each other and are called co-worker classes.
An implementation that does not use a mediator:

AbstractColleague

/**
 * @author lijun
 * @since 2018-04-09 9:37
 */
public abstract class AbstractColleague {
    protected int number;    

    public int getNumber() {    
        return number;    
    }    

    public void setNumber(int number){    
        this.number = number;    
    }
    /**
     * 抽象方法,修改数字时同时修改关联对象
     */
    public abstract void setNumber(int number, AbstractColleague coll);
}    

ConcreteColleagueA

/**
 * @author lijun
 * @since 2018-04-09 9:37
 */
public class ConcreteColleagueA extends AbstractColleague{
    @Override
    public void setNumber(int number, AbstractColleague coll) {
        this.number = number;    
        coll.setNumber(number*100);    
    }    
}    

ConcreteColleagueB

/**
 * @author lijun
 * @since 2018-04-09 9:37
 */
public class ConcreteColleagueB extends AbstractColleague{

    @Override
    public void setNumber(int number, AbstractColleague coll) {
        this.number = number;    
        coll.setNumber(number/100);    
    }    
} 

Client

public class Client {
    public static void main(String[] args){    

        AbstractColleague collA = new ConcreteColleagueA();
        AbstractColleague collB = new ConcreteColleagueB();

        System.out.println("==========设置A影响B==========");    
        collA.setNumber(1288, collB);    
        System.out.println("collA的number值:"+collA.getNumber());    
        System.out.println("collB的number值:"+collB.getNumber());    

        System.out.println("==========设置B影响A==========");    
        collB.setNumber(87635, collA);    
        System.out.println("collB的number值:"+collB.getNumber());    
        System.out.println("collA的number值:"+collA.getNumber());    
    }    
}   

output

==========设置A影响B==========
collA的number值:1288
collB的number值:128800
==========设置B影响A==========
collB的number值:87635
collA的number值:876

In the above code, class A and class B are related through direct association. If we want to use the intermediary model, class A and class B cannot be directly related. They must be related through an intermediary to achieve the purpose of association. .

Mediator mediator interface

/**
 *  中介者接口
 * @author lijun
 * @since 2018-04-09 9:34
 */
public interface Mediator {

    /**
     *影响A
     */
    public void  affectA();

    /**
     *
     */
    public void  affectB();
}

ConcreteMediator concrete mediator

/**
 * 具体的中介者
 *
 * @author lijun
 * @since 2018-04-09 9:37
 */
public class ConcreteMediator implements Mediator {

    /**
     * 中介者持有同事类A
     */
    ConcreteColleagueA  concreteColleagueA;

    /**
     * 中介者持有同类B
     */
    ConcreteColleagueB concreteColleagueB;


    public void setConcreteColleagueA(ConcreteColleagueA concreteColleagueA) {
        this.concreteColleagueA = concreteColleagueA;
    }

    public void setConcreteColleagueB(ConcreteColleagueB concreteColleagueB) {
        this.concreteColleagueB = concreteColleagueB;
    }

    /**
     * 影响A
     */
    @Override
    public void affectA() {
        concreteColleagueA.setNumber(concreteColleagueB.getNumber()/100);
    }

    /**
     *
     */
    @Override
    public void affectB() {
        concreteColleagueB.setNumber(concreteColleagueA.getNumber()*100);
    }
}

AbstractColleague colleague abstract class

/**
 * 同事抽象类
 * @author lijun
 * @since 2018-04-09 9:34
 */
public abstract class AbstractColleague {
    protected int number;
    private Mediator mediator;

    public AbstractColleague(Mediator mediator) {
        this.mediator = mediator;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public Mediator getMediator() {
        return mediator;
    }

    public void setMediator(Mediator mediator) {
        this.mediator = mediator;
    }

    /**
     * 设置number的值
     *
     * @param number   值
     * @param mediator 中介者
     */
    public abstract void setNumber(int number, Mediator mediator);

}

ConcreteColleagueA Colleague Class A

/**
 * 同事类A 设置同事类A的值影响B的值
 * 通过中介者来改变B的值
 *
 * @author lijun
 * @since 2018-04-09 9:42
 */
public class ConcreteColleagueA extends AbstractColleague {

    public ConcreteColleagueA(Mediator mediator) {
        super(mediator);
    }
    /**
     * 设置number的值
     *
     * @param number   值
     * @param mediator 中介者
     */
    @Override
    public void setNumber(int number, Mediator mediator) {
        this.number = number;
        mediator.affectB();
    }
}

ConcreteColleagueB Colleague Class B

/**
 * 同事类B 设置同事类B的值影响B的值
 * 通过中介者来改变A的值
 *
 * @author lijun
 * @since 2018-04-09 9:42
 */
public class ConcreteColleagueB extends AbstractColleague {

    public ConcreteColleagueB(Mediator mediator) {
        super(mediator);
    }

    /**
     * 设置number的值
     *
     * @param number   值
     * @param mediator 中介者
     */
    @Override
    public void setNumber(int number, Mediator mediator) {
        this.number = number;
        mediator.affectA();
    }
}

Client

public class Client {
    public static void main(String[] args) {

        //中介者
        ConcreteMediator mediator = new ConcreteMediator();

        //同事对象
        ConcreteColleagueA concreteColleagueA = new ConcreteColleagueA(mediator);
        ConcreteColleagueB concreteColleagueB = new ConcreteColleagueB(mediator);

        //中介者设置同事对象
        mediator.setConcreteColleagueA(concreteColleagueA);
        mediator.setConcreteColleagueB(concreteColleagueB);

        System.out.println( "==========设置A影响B==========");
        concreteColleagueA.setNumber(100, mediator);
        System.out.println("concreteColleagueA: "+concreteColleagueA.getNumber());
        System.out.println("concreteColleagueB: "+concreteColleagueB.getNumber());

        System.out.println( "==========设置B影响A==========");
        concreteColleagueB.setNumber(1000, mediator);

        System.out.println("concreteColleagueB: "+concreteColleagueB.getNumber());
        System.out.println("concreteColleagueA: "+concreteColleagueA.getNumber());

    }
}

output:

==========设置A影响B==========
concreteColleagueA: 100
concreteColleagueB: 10000
==========设置B影响A==========
concreteColleagueB: 1000
concreteColleagueA: 10

4. Essence

encapsulate interaction

5. Advantages and disadvantages and summary

1. Advantages

  • Proper use of the mediator pattern can avoid excessive coupling between colleague classes, so that each colleague class can be used relatively independently.
  • Using the mediator pattern can transform the one-to-many association between objects into a one-to-one association, making the relationship between objects easy to understand and maintain.
  • Using the mediator pattern can abstract the behavior and cooperation of objects, and can deal with the interaction between objects more flexibly.

2. Disadvantages

  • over-centralized. If the interaction of colleague objects is very large, and it is relatively complex, when this complexity is all concentrated in the mediator, the mediator object will become very complex, and it is difficult to manage and maintain.

3. Summary

In object-oriented programming, a class is bound to have dependencies with other classes, and a completely independent class is meaningless. It is also quite common for a class to depend on multiple classes at the same time. Since such a situation exists, it means that the one-to-many dependency relationship has its rationale. Proper use of the mediator pattern can make the originally messy object relationship clear, but if Misuse may have the opposite effect. In general, the mediator pattern is only considered for relationships that are networked between peer classes. The net-like structure can be changed into a star-like structure to make the relationship between colleague classes clearer. The mediator mode is a relatively common mode, and it is also a mode that is easily abused. For most cases, the relationship between colleague classes will not be complicated to a chaotic network structure. Therefore, in most cases, it is enough to encapsulate the dependencies between objects inside the colleague class, and there is no need to introduce intermediaries. user mode. Abusing the mediator model will only make things more complicated.

Source address

Guess you like

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