设计模式 - 中介者模式

306 阅读3分钟

小知识,大挑战!本文正在参与 “程序员必备小知识” 创作活动

初衷 :因架构(开发)场景(需求)而使用设计模式,莫为了使用设计模式而设计架构场景!

在我认为,设计模式本身就是用于开阔开发者的思维的,所以思想决定出路咯 ~

设计模式共23种,分为三种类型

  • 创建型模式:单例模式、工厂模式、抽象工厂模式、建造者模式、原型模式

  • 结构型模式:代理模式、装饰模式、外观模式、享元模式、桥接模式、组合模式、适配器模式

  • 行为型模式:观察者模式、策略模式、中介者模式、模版方法模式、命令模式、迭代器模式、职责链模式(责任链模式)、备忘录模式、解释器模式(Interpreter模式)、状态模式、访问者模式\

基本概念

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

优缺点

中介者模式更像MVC的开发框架,C 就是中介者,主要是高内聚,低耦合 ~

优点
  • 减少类间依赖,降低耦合性
  • 符合迪米特原则
缺点
  • 中后期代码太多,逻辑复杂
使用场景

系统中对象之间存在比较复杂的引用关系

想通过一个中间类来封装多个类的行为,而又不想生成太多的子类

中介者模式与代理模式的区别

当中介者中管理的类少的时候,总感觉有些像代理模式,这其实是由于中介者模式是降低一些类之间耦合性,当只降低两个类的耦合性时,写法上跟代理模式就差不多了,但是基本思想还是不一样的

中介者模式中各个类之间是平级的,StudentA,StudentB等,但是代理模式不是,其实解耦的两个类应该是调用关系

角色分配 (示范代码)

效果

在这里插入图片描述

抽象中介者 (Mediator)
package com.example.yongliu.mediatorpatter;

/**
 * author  yongliu
 * date  2018/3/6.
 * desc:
 */

public interface Mediator {
    void talk(String message,Colleague colleague);
}
具体中介者 (SpecificMediator)
package com.example.yongliu.mediatorpatter;

/**
 * author  yongliu
 * date  2018/3/6.
 * desc:
 */
public class SpecificMediator implements Mediator {
	//定义具体同事类 - FatherColleague
    public  FatherColleague mFatherColleague;
    //定义具体同事类 - MatherColleague 
    public  MatherColleague mMatherColleague;

    public void setmFatherColleague(FatherColleague mFatherColleague) {
        this.mFatherColleague = mFatherColleague;
    }

    public void setmMatherColleague(MatherColleague mMatherColleague) {
        this.mMatherColleague = mMatherColleague;
    }
    
    public FatherColleague getmFatherColleague() {
        return mFatherColleague;
    }

    public MatherColleague getmMatherColleague() {
        return mMatherColleague;
    }
    
    @Override
    public void talk(String message, Colleague colleague) {
        if(colleague==mFatherColleague){
            mFatherColleague.getMessage(message);
        }else{
            mMatherColleague.getMessage(message);
        }
    }
}
抽象同事类(Colleague)
package com.example.yongliu.mediatorpatter;

/**
 * author  yongliu
 * date  2018/3/6.
 * desc:
 */
public abstract class Colleague {
    public Mediator mediator;
    public String mQuestion;

    public Colleague(String question ,Mediator mMediator){
        this.mQuestion=question;
        this.mediator=mMediator;
    }
}
具体同事类
  • FatherColleague
package com.example.yongliu.mediatorpatter;

import android.util.Log;

/**
 * author  yongliu
 * date  2018/3/6.
 * desc:
 */
public class FatherColleague extends Colleague {

    public FatherColleague(String question,Mediator mMediator) {
        super(question,mMediator);
    }

    public void contact(String data){
           mediator.talk(data,this);
    }

    public void getMessage(String msg) {
        Log.e("tag","Android Developer#"+mQuestion+"#:"+msg);
    }
}
  • MatherColleague
package com.example.yongliu.mediatorpatter;

import android.util.Log;

/**
 * author  yongliu
 * date  2018/3/6.
 * desc:
 */
public class MatherColleague extends Colleague{

    public MatherColleague(String question,Mediator mMediator) {
        super(question,mMediator);
    }

    public void contact(String data) {
        mediator.talk(data,this);
    }

    public void getMessage(String msg) {
        Log.e("tag","Android Developer#"+mQuestion+"#:"+msg);
    }
}
客户端(使用处)
package com.example.yongliu.mediatorpatter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView mBtn = findViewById(R.id.btn);

        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SpecificMediator specificMediator = new SpecificMediator();
                FatherColleague fatherColleague = new FatherColleague("Father角色",specificMediator);
                MatherColleague matherColleague = new MatherColleague("Mather角色",specificMediator);

                specificMediator.setmFatherColleague(fatherColleague);
                specificMediator.setmMatherColleague(matherColleague);

                fatherColleague.contact("死了都要爱");
                matherColleague.contact("不哭到微笑不痛快");
            }
        });
    }
}