大话设计模式-中介者模式-2020-10-28

2020-10-29  本文已影响0人  勇往直前888

定义

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

结构图

image.png

使用场景

联合国的例子

image.png
/**
 * 联合国;Mediator
 */
abstract class UnitedNations {
    // 声明,也就是国家在联合国发言
    public abstract void declare(String message, Country country);
}
/**
 * 国家;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
 */
class USA extends Country {
    public USA(UnitedNations unitedNations) {
        super(unitedNations);
        name = "美国";
    }
}

/**
 * 伊拉克;ConcreteCollege
 */
class Iraq extends Country {
    public Iraq(UnitedNations unitedNations) {
        super(unitedNations);
        name = "伊拉克";
    }
}
/**
 * 安理会;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);
        }
    }
}
image.png
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

上一篇下一篇

猜你喜欢

热点阅读