大话设计模式-中介者模式-2020-10-28
2020-10-29 本文已影响0人
勇往直前888
定义
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
结构图
image.png使用场景
-
中介者模式一般应用于一组对象以定义良好但是复杂的方式进行通信的场合,以及想定制一个分布在多个类中的行为,而又不想生成太多的子类的场合。
-
中介者模式很容易在系统中应用,也很容易误用。当系统出现多对多交互复杂的对象群时,不要急于使用中介者模式,而是先改进系统的设计。
-
Mediator的出现减少了各个Colleague的耦合,使得可以独立地改变和复用各个Colleague类和Mediator。
-
由于把对象如何协作进行了抽象,将中介作为一个独立的概念并将其封装在一个对象中,这样关注的对象就从对象各自本身的行为转移到它们之间的交互上来,也就是站在一个更宏观的角度去看待系统。
-
由于ConcreteMediator控制了集中化,于是就把交互复杂性变为了中介者的复杂性,这就使得中介者会变得比任何一个ConcreteColleague都复杂。
联合国的例子
- 结构图
- UnitedNations;联合国;Mediator
/**
* 联合国;Mediator
*/
abstract class UnitedNations {
// 声明,也就是国家在联合国发言
public abstract void declare(String message, Country country);
}
- Country;国家;Colleague;
/**
* 国家;Colleague;
* abstract 只是为了防止实例化
*/
abstract class Country {
protected String name = "国家的名字";
// 收到的消息
protected String receivedMessage = "";
protected UnitedNations unitedNations;
public Country(UnitedNations unitedNations) {
this.unitedNations = unitedNations;
}
// 声明,通过联合国传递出去
public void declare(String message) {
unitedNations.declare(message, this);
}
// 接收消息;一般供联合国(中介者)调用;
public void getMessage(String message, String sender) {
receivedMessage = name + "收到来自" + sender + "的信息:" + message + "\n";
}
// 名字getter;一般给联合国用
public String getName() {
return name;
}
// 展示收到的消息
public String showReceivedMessage() {
return receivedMessage;
}
}
- 具体的国家;ConcreteCollege
/**
* 美国;ConcreteCollege
*/
class USA extends Country {
public USA(UnitedNations unitedNations) {
super(unitedNations);
name = "美国";
}
}
/**
* 伊拉克;ConcreteCollege
*/
class Iraq extends Country {
public Iraq(UnitedNations unitedNations) {
super(unitedNations);
name = "伊拉克";
}
}
- SecurityCouncil;安理会;ConcreteMediator;
/**
* 安理会;ConcreteMediator;这里专门处理美国和伊拉克的交互
*/
class SecurityCouncil extends UnitedNations {
public USA usa;
public Iraq iraq;
@Override
public void declare(String message, Country country) {
// 作为中间人,传话
if (country == usa) {
iraq.getMessage(message, usa.name);
}
if (country == iraq) {
usa.getMessage(message, iraq.name);
}
}
}
- 测试界面
- 客户端代码
public class MediatorActivity extends AppCompatActivity {
public static void launch(Context context) {
if (null != context) {
Intent intent = new Intent();
intent.setClass(context, MediatorActivity.class);
if (!(context instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
context.startActivity(intent);
}
}
SecurityCouncil securityCouncil;
USA usa;
Iraq iraq;
EditText editTextUSA;
EditText editTextIraq;
TextView textViewDeclare;
String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mediator);
setTitle("中介者模式");
securityCouncil = new SecurityCouncil();
usa = new USA(securityCouncil);
iraq = new Iraq(securityCouncil);
securityCouncil.usa = usa;
securityCouncil.iraq = iraq;
editTextUSA = findViewById(R.id.editTextUSA);
editTextIraq = findViewById(R.id.editTextIraq);
textViewDeclare = findViewById(R.id.textViewDeclare);
message = "";
}
public void onClearClick(View view) {
message = "";
textViewDeclare.setText(message);
}
public void onUSAClick(View view) {
// 美国声明;安理会转给伊拉克
usa.declare(editTextUSA.getText().toString());
// 展示伊拉克收到的信息
message += iraq.showReceivedMessage();
textViewDeclare.setText(message);
}
public void onIraqClick(View view) {
// 伊拉克声明,安理会转给美国
iraq.declare(editTextIraq.getText().toString());
// 展示美国收到的信息
message += usa.showReceivedMessage();
textViewDeclare.setText(message);
}
}
Demo地址
https://gitee.com/zhangxusong888/Android/tree/master/design_pattern