中介者模式

基础

定义: 用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互。 类型: 行为类模式 关键点: 用一个中介对象来封装一系列的对象交互。

类图


Java代码

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, AbstractMediator am);
}
class ColleagueA extends AbstractColleague {
    @Override
    public void setNumber(int number, AbstractMediator am) {
        this.number = number;
        am.aAffectB();
    }
}
class ColleagueB extends AbstractColleague {
    @Override
    public void setNumber(int number, AbstractMediator am) {
        this.number = number;
        am.bAffectA();
    }
}
abstract class AbstractMediator {
    protected AbstractColleague a;
    protected AbstractColleague b;
    public AbstractMediator(AbstractColleague a, AbstractColleague b) {
        this.a = a;
        this.b = b;
    }
    public abstract void aAffectB();
    public abstract void bAffectA();
}
class Mediator extends AbstractMediator {
    public Mediator(AbstractColleague a, AbstractColleague b) {
        super(a, b);
    }
    @Override
    public void aAffectB() {
        int number = a.getNumber();
        b.setNumber(number * 10);
    }
    @Override
    public void bAffectA() {
        int number = b.getNumber();
        a.setNumber(number / 10);
    }
}
public class Client {
    public static void main(String[] args) {
        AbstractColleague collA = new ColleagueA();
        AbstractColleague collB = new ColleagueB();
        AbstractMediator am = new Mediator(collA, collB);
        System.out.println("===== 通过设置A影响B =====");
        collA.setNumber(1000, am);
        System.out.println("collA的number值为:" + collA.getNumber());
        System.out.println("collB的number值为A的10倍:" + collB.getNumber());
        System.out.println("===== 通过设置B影响A =====");
        collB.setNumber(1000, am);
        System.out.println("collB的number值为:" + collB.getNumber());
        System.out.println("collA的number值为B的0.1倍:" + collA.getNumber());
    }
}

PHP代码

abstract class AbstractColleague
{
    protected $number;
    public function getNumber()
    {
        return $this->number;
    }
    public function setNumber($number)
    {
        $this->number = $number;
    }
    abstract public function setAffectNumber($number, AbstractMediator $am);
}
class ColleagueA extends AbstractColleague
{
    public function setAffectNumber($number, AbstractMediator $am)
    {
        $this->number = $number;
        $am->aAffectB();
    }
}
class ColleagueB extends AbstractColleague
{
    public function setAffectNumber($number, AbstractMediator $am)
    {
        $this->number = $number;
        $am->bAffectA();
    }
}
abstract class AbstractMediator
{
    protected $a;
    protected $b;
    public function __construct(AbstractColleague $a, AbstractColleague $b)
    {
        $this->a = $a;
        $this->b = $b;
    }
    abstract public function aAffectB();
    abstract public function bAffectA();
}
class Mediator extends AbstractMediator
{
    public function aAffectB()
    {
        $number = $this->a->getNumber();
        $this->b->setNumber($number * 10);
    }
    public function bAffectA()
    {
        $number = $this->b->getNumber();
        $this->a->setNumber($number / 10);
    }
}
class MediatorTest extends TestCase
{
    public function test()
    {
        $collA = new ColleagueA();
        $collB = new ColleagueB();
        $am = new Mediator($collA, $collB);
        echo "===== 通过设置A影响B =====\n";
        $collA->setAffectNumber(1000, $am);
        echo "collA的number值为:" . $collA->getNumber() . "\n";
        echo "collB的number值为A的10倍:" . $collB->getNumber() . "\n";
        echo "===== 通过设置B影响A =====\n";
        $collB->setAffectNumber(1000, $am);
        echo "collB的number值为:" . $collB->getNumber() . "\n";
        echo "collA的number值为B的0.1倍:" . $collA->getNumber() . "\n";
    }
}
执行结果

===== 通过设置A影响B =====
collA的number值为:1000
collB的number值为A的10倍:10000
===== 通过设置B影响A =====
collB的number值为:1000
collA的number值为B的0.1倍:100
参考文档