设计模式8 ——中介者模式

一、中介者模式的定义 

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently(用一个中介对象封装一系列对象交互,中介者使各对象不需要显示地相互作用,从而使其耦合松散,而且可以独立地改变他们之间的交互)。


二、中介者模式的应用
1、中介者模式的优点

中介者模式的有点就是减少类间的依赖,把所有的一对多的依赖变成一对一的依赖,同事类只依中介者,减少了依赖,当然同事也降低了类间的耦合。


2、中介者模式的缺点

中介者模式的缺点就是中介者会膨胀的很大,而且逻辑复杂。原本N个对象直接的相互依赖关系转换为中介者和同事类的依赖关系,同事类越多,中介者的逻辑就越复杂。

3、中介者模式的使用场景

中介者适用于多个对象之间紧密耦合的情况。紧密耦合的标准是:在类图中出现了蜘蛛网状结构。在这种情况下一定要考虑使用中介者模式,这有利于把蜘蛛网梳理为星型结构,使原本复杂混乱的关系变得清晰简单。当对象之间存在复杂的网状结构关系而导致依赖关系混乱且难以复用时,以及当想创建一个运行于多个类之间的对象,又不想生成新的子类时。

 三、中介者模式的写法

public abstract class Mediator {
    //具体的同事类
    protected ConcreteColleague1 colleague1;
    protected ConcreteColleague2 colleague2;
    public void setColleague1(ConcreteColleague1 colleague1){
        this.colleague1 = colleague1;
    }
    public void setColleague2(ConcreteColleague2 colleague2){
        this.colleague2 = colleague2;
    }
    //中介者模式的业务逻辑
    public abstract void doSomething1();
    public abstract void doSomething2();
}

public class ConcreteMediator extends Mediator{

    @Override
    public void doSomething1() {
        super.colleague2.selfMethod();
    }
    @Override
    public void doSomething2() {
        super.colleague1.selfMethod();
    }
}

public abstract class Colleague {
    protected Mediator mediator;
    public Colleague(Mediator mediator){
        this.mediator = mediator;
    }
}

public class ConcreteColleague1 extends Colleague{
    public ConcreteColleague1(Mediator mediator) {
        super(mediator);
        mediator.setColleague1(this);

    }
    public void selfMethod(){
        //该类自己的业务逻辑
         Log.e("TAG","This is a self-method");
    }
    public void depMethod(){
        //该类与其它类有关联的业务逻辑
        super.mediator.doSomething1();
    }
}

public class ConcreteColleague2 extends Colleague{

    public ConcreteColleague2(Mediator mediator) {
        super(mediator);
        mediator.setColleague2(this);
    }
    public void selfMethod(){
       Log.e("TAG","This is a self method in colleague2");
    }
    public void depMethod(){
       Log.e("TAG","This is a dependent method in colleague2");
        super.mediator.doSomething2();
    }
}

public class Client {

    public static void main(String[] args) {   
        Mediator mediator = new ConcreteMediator();
        ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
        ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);
        colleague1.depMethod();
        colleague2.depMethod();
    }
}

四、中介者模式在源码中的应用

public class KeyguardViewMediator extends SystemUI {
    ...
    private AlarmManager mAlarmManager;
    private AudioManager mAudioManager;
    private StatusBarManager mStatusBarManager;
    ...
    private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
    ...
    private void playSounds(boolean locked) {
        playSound(locked ? mLockSoundId : mUnlockSoundId);
    }

    private void playSound(int soundId) {
        if (soundId == 0) return;
        final ContentResolver cr = mContext.getContentResolver();
        if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1) {

            mLockSounds.stop(mLockSoundStreamId);
            // Init mAudioManager
            if (mAudioManager == null) {
                mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
                if (mAudioManager == null) return;
                mUiSoundsStreamType = mAudioManager.getUiSoundsStreamType();
            }

            mUiOffloadThread.submit(() -> {
                // If the stream is muted, don't play the sound
                if (mAudioManager.isStreamMute(mUiSoundsStreamType)) return;

                int id = mLockSounds.play(soundId,
                        mLockSoundVolume, mLockSoundVolume, 1/*priortiy*/, 0/*loop*/, 1.0f/*rate*/);
                synchronized (this) {
                    mLockSoundStreamId = id;
                }
            });

        }
    }
    ...
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值