首页 > 代码库 > 中介者模式

中介者模式

中介者模式(Mediator),用一个中介对象来封装一系列的对象交互。中介对象使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

Mediator类

package mediator;
/**
 * 抽象中介者类
 * 原来是同事A、B、C、D之间直接发送消息的
 * 现在模式是:同事A、B、C、D不直接交互,而是通过中介者来传递消息
 * 所以抽象中介者要在方法中表明是谁发出消息,由谁来接受消息
 * @author 煞笔
 *
 */
public abstract class Mediator {
    //方法中指出那个同事是发出消息的,那位同事来接受消息
    public abstract void send(String message, Colleague send_colleague,Colleague accept_colleague);
}

ConcreteMediator类

package mediator;
/**
 * 具体中介者类
 * 中介者要在方法中表明是谁发出消息,由谁来接受消息
 * @author 煞笔
 *
 */
public class ConcreteMediator extends Mediator {
    @Override                                                
    public void send(String message, Colleague send_colleague,Colleague accept_colleague) {
        //接受者收到发送者发出的消息
        accept_colleague.accept(message,send_colleague);
    }
}

Colleague类

package mediator;
/**
 * 抽象同事类
 * 同事A、B、C、D不直接交互,而是通过中介者来传递消息
 * 要达到这种效果,同事们都需要认识中介者
 * 每位同事都具有发出消息,接收消息的功能
 * @author 煞笔
 *
 */
public abstract class Colleague {
    protected Mediator mediator;//认识中介者
    private String name;

    public Colleague(Mediator mediator,String name) {
        this.mediator = mediator;
        this.name = name;
    }
    
    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }

    /**
     * 发送消息给接受同事
     * @param message
     * @param colleague
     */
    public abstract void send(String message,Colleague accept_colleague);
    
    /**
     * 从发出消息的同事那里接收到消息
     * @param message
     * @param from_colleague
     */
    public abstract void accept(String message,Colleague send_colleague);
}

ConcreateColleagueA类

package mediator;

public class ConcreateColleagueA extends Colleague {


    public ConcreateColleagueA(Mediator mediator, String name) {
        super(mediator, name);
    }

    @Override
    public void send(String message,Colleague accept_colleague) {
        System.out.println(message);
        mediator.send(message, this,accept_colleague);
    }

    @Override
    public void accept(String message,Colleague send_colleague) {
        System.out.println("接受者A收到信息了,消息来自"+send_colleague.getName());
    }

}

ConcreateColleagueB类

package mediator;

public class ConcreateColleagueB extends Colleague {


    public ConcreateColleagueB(Mediator mediator, String name) {
        super(mediator, name);
    }

    @Override
    public void send(String message,Colleague accept_colleague) {
        System.out.println(message);
        mediator.send(message, this,accept_colleague);
    }

    @Override
    public void accept(String message,Colleague send_colleague) {
        System.out.println("接受者B收到信息了,消息来自"+send_colleague.getName());
    }

}

ConcreateColleagueC类

package mediator;

public class ConcreateColleagueC extends Colleague {


    public ConcreateColleagueC(Mediator mediator, String name) {
        super(mediator, name);
    }

    @Override
    public void send(String message,Colleague accept_colleague) {
        System.out.println(message);
        mediator.send(message, this,accept_colleague);
    }

    @Override
    public void accept(String message,Colleague send_colleague) {
        System.out.println("接受者C收到信息了,消息来自"+send_colleague.getName());
    }

}

ConcreateColleagueD类

package mediator;

public class ConcreateColleagueD extends Colleague {


    public ConcreateColleagueD(Mediator mediator, String name) {
        super(mediator, name);
    }

    @Override
    public void send(String message,Colleague accept_colleague) {
        System.out.println(message);
        mediator.send(message, this,accept_colleague);
    }

    @Override
    public void accept(String message,Colleague send_colleague) {
        System.out.println("接受者D收到信息了,消息来自"+send_colleague.getName());
    }

}

Business类

package mediator;

public class Business {

    public static void main(String[] args) {
        ConcreteMediator mediator = new ConcreteMediator();
        Colleague colleagueA = new ConcreateColleagueA(mediator,"A");
        Colleague colleagueB = new ConcreateColleagueB(mediator,"B");
        Colleague colleagueC = new ConcreateColleagueC(mediator,"C");
        Colleague colleagueD = new ConcreateColleagueD(mediator,"D");
        colleagueA.send("我是A,讲个美人鱼故事,B接受的到了吗",colleagueB);
        System.out.println("---------1个发送接受过程结束--------------");
        colleagueB.send("我是B,早上路上较堵,注意安全,A接受的到了吗",colleagueA);
        System.out.println("---------2个发送接受过程结束--------------");
        colleagueC.send("我是C,我有话对说,B接受的到了吗",colleagueB);
        System.out.println("---------3个发送接受过程结束--------------");
        colleagueD.send("我是D,周末出去玩吗,A接受的到了吗",colleagueA);
        System.out.println("---------4个发送接受过程结束--------------");
        colleagueA.send("我是A,讲个美人鱼故事,C接受的到了吗",colleagueC);
        System.out.println("---------5个发送接受过程结束--------------");
        colleagueB.send("我是B,早上路上较堵,注意安全,D接受的到了吗",colleagueD);
        System.out.println("---------6个发送接受过程结束,后面还有很多组合,4个人,随意其中2个人都可以交互--------------");
    }

}

 

中介者模式