前言
提示:以下是本篇文章正文内容,下面案例可供参考
一、中介者模式基本介绍
二、应用实例-智能家庭管理
文章来源:https://www.toymoban.com/news/detail-431437.html
完整代码
抽象中介者 Mediator
package tanchishell.SJMS.smarthouse;
//抽象中介者
public abstract class Mediator {
//将给中介者对象,加入到集合中
public abstract void Register(String colleagueName, Colleague colleague);
//接收消息, 具体的同事对象发出
public abstract void GetMessage(int stateChange, String colleagueName);
public abstract void SendMessage();
}
具体中介者 ConcreteMediator
package tanchishell.SJMS.smarthouse;
import java.util.HashMap;
//具体的中介者类
public class ConcreteMediator extends Mediator {
//集合,放入所有的同事对象
private HashMap<String, Colleague> colleagueMap;
private HashMap<String, String> interMap;
public ConcreteMediator() {
colleagueMap = new HashMap<String, Colleague>();
interMap = new HashMap<String, String>();
}
@Override
public void Register(String colleagueName, Colleague colleague) {
colleagueMap.put(colleagueName, colleague);
if (colleague instanceof Alarm) {
interMap.put("Alarm", colleagueName);
} else if (colleague instanceof CoffeeMachine) {
interMap.put("CoffeeMachine", colleagueName);
} else if (colleague instanceof TV) {
interMap.put("TV", colleagueName);
} else if (colleague instanceof Curtains) {
interMap.put("Curtains", colleagueName);
}
}
//具体中介者的核心方法
//1. 根据得到消息,完成对应任务
//2. 中介者在这个方法,协调各个具体的同事对象,完成任务
@Override
public void GetMessage(int stateChange, String colleagueName) {
//处理闹钟发出的消息
if (colleagueMap.get(colleagueName) instanceof Alarm) {
if (stateChange == 0) {
((CoffeeMachine) (colleagueMap.get(interMap
.get("CoffeeMachine")))).StartCoffee();
((TV) (colleagueMap.get(interMap.get("TV")))).StartTv();
} else if (stateChange == 1) {
((TV) (colleagueMap.get(interMap.get("TV")))).StopTv();
}
} else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {
((Curtains) (colleagueMap.get(interMap.get("Curtains"))))
.UpCurtains();
} else if (colleagueMap.get(colleagueName) instanceof TV) {//如果 TV 发现消息
} else if (colleagueMap.get(colleagueName) instanceof Curtains) {
//如果是以窗帘发出的消息,这里处理... }
}
}
@Override
public void SendMessage() {
}
}
抽象同事类 Colleague
package tanchishell.SJMS.smarthouse;
//同事抽象类
public abstract class Colleague {
private Mediator mediator;
public String name;
public Colleague(Mediator mediator, String name) {
this.mediator = mediator;
this.name = name;
}
public Mediator GetMediator() {
return this.mediator;
}
public abstract void SendMessage(int stateChange);
}
具体同事类
闹钟 Alarm
package tanchishell.SJMS.smarthouse;
//具体的同事类 闹钟
public class Alarm extends Colleague {
//构造器
public Alarm(Mediator mediator, String name){
super(mediator, name);
//在创建 Alarm 同事对象时,将自己放入到 ConcreteMediator 对象中[集合]
mediator.Register(name, this);
}
public void SendAlarm(int stateChange) {
SendMessage(stateChange);
}
@Override
public void SendMessage(int stateChange) {
//调用的中介者对象的 getMessage
this.GetMediator().GetMessage(stateChange, this.name);
}
}
咖啡机 CoffeeMachine
package tanchishell.SJMS.smarthouse;
//具体的同事类 咖啡机
public class CoffeeMachine extends Colleague{
public CoffeeMachine(Mediator mediator, String name) {
super(mediator, name);
mediator.Register(name, this);
}
@Override
public void SendMessage(int stateChange) {
this.GetMediator().GetMessage(stateChange, this.name);
}
public void StartCoffee() {
System.out.println("咖啡机开始工作");
}
public void FinishCoffee() {
System.out.println("After 5 minutes!");
System.out.println("Coffee is ok!");
SendMessage(0);
}
}
TV
package tanchishell.SJMS.smarthouse;
public class TV extends Colleague {
public TV(Mediator mediator, String name) {
super(mediator, name);
// TODO Auto-generated constructor stub
mediator.Register(name, this);
}
@Override
public void SendMessage(int stateChange) {
this.GetMediator().GetMessage(stateChange, this.name);
}
public void StartTv() {
System.out.println("It's time to 打开电视机!");
}
public void StopTv() {
System.out.println("StopTv!");
}
}
窗帘 Curtains
package tanchishell.SJMS.smarthouse;
public class Curtains extends Colleague {
public Curtains(Mediator mediator, String name) {
super(mediator, name);
mediator.Register(name, this);
}
@Override
public void SendMessage(int stateChange) {
this.GetMediator().GetMessage(stateChange, this.name);
}
public void UpCurtains() {
System.out.println("I am holding 生气(升起)窗帘!");
}
}
测试 Clint
package tanchishell.SJMS.smarthouse;
public class ClientTest {
public static void main(String[] args) {
//创建一个中介者对象
Mediator mediator = new ConcreteMediator();
//创建 Alarm 并且加入到 ConcreteMediator 对象的 HashMap
Alarm alarm = new Alarm(mediator, "alarm");
//创建了 CoffeeMachine 对象,并 且加入到 ConcreteMediator 对象的 HashMap
CoffeeMachine coffeeMachine = new CoffeeMachine(mediator, "coffeeMachine");
//创建 Curtains , 并 且加入到 ConcreteMediator 对象的 HashMap
Curtains curtains = new Curtains(mediator, "curtains");
TV tV = new TV(mediator, "TV");
//让闹钟发出消息
alarm.SendAlarm(0);
// coffeeMachine.FinishCoffee();
// alarm.SendAlarm(1);
}
}
输出
咖啡机开始工作
It's time to 打开电视机!
After 5 minutes!
Coffee is ok!
I am holding 生气(升起)窗帘!
StopTv!
三、中介者模式的注意事项和细节
文章来源地址https://www.toymoban.com/news/detail-431437.html
到了这里,关于第二十章 中介者模式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!