package hrh.study.BehavioralMode.MediatorPattern;
/**
* 中介者模式
*/
/**
* 同事类接口
*/
interface Light {
void on();
void off();
void setMediator(LightMediator mediator);
}
/**
* 具体同事类:客厅灯光
*/
class LivingRoomLight implements Light {
private LightMediator mediator;
@Override
public void on() {
System.out.println("Living Room Light is ON");
}
@Override
public void off() {
System.out.println("Living Room Light is OFF");
}
@Override
public void setMediator(LightMediator mediator) {
this.mediator = mediator;
}
}
/**
* 具体同事类:厨房灯光
*/
class KitchenLight implements Light {
private LightMediator mediator;
@Override
public void on() {
System.out.println("Kitchen Light is ON");
}
@Override
public void off() {
System.out.println("Kitchen Light is OFF");
}
@Override
public void setMediator(LightMediator mediator) {
this.mediator = mediator;
}
}
/**
* 中介者接口
*/
interface LightMediator {
void turnOn(Light light);
void turnOff(Light light);
}
/**
* 具体中介者类
*/
class SimpleLightMediator implements LightMediator {
@Override
public void turnOn(Light light) {
light.on();
}
@Override
public void turnOff(Light light) {
light.off();
}
}
/**
* 客户端代码
*/
public class Client {
public static void main(String[] args) {
LightMediator mediator = new SimpleLightMediator();
Light livingRoomLight = new LivingRoomLight();
Light kitchenLight = new KitchenLight();
livingRoomLight.setMediator(mediator);
kitchenLight.setMediator(mediator);
mediator.turnOn(livingRoomLight); // 开启客厅灯光
mediator.turnOn(kitchenLight); // 开启厨房灯光
mediator.turnOff(livingRoomLight); // 关闭客厅灯光
mediator.turnOff(kitchenLight); // 关闭厨房灯光
}
}
hrhcode
- 粉丝: 299
- 资源: 1
最新资源
- numpy-2.2.0-cp313-cp313-macosx_14_0_x86_64.whl
- numpy-2.2.0-cp313-cp313-macosx_11_0_arm64.whl
- numpy-2.2.0-cp313-cp313-musllinux_1_2_aarch64.whl
- numpy-2.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- numpy-2.2.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- numpy-2.2.0-cp313-cp313-win32.whl
- numpy-2.2.0-cp313-cp313-win_amd64.whl
- numpy-2.2.0-cp313-cp313-musllinux_1_2_x86_64.whl
- numpy-2.2.0-cp313-cp313t-macosx_14_0_arm64.whl
- 简单4步-利用ArcGIS处理nc格式文件得到任意区域的掩膜文件方法详解-附带制作好的【中国+7大子区域】掩膜nc格式文件
- numpy-2.2.0-cp313-cp313t-macosx_11_0_arm64.whl
- numpy-2.2.0-cp313-cp313t-macosx_10_13_x86_64.whl
- numpy-2.2.0-cp313-cp313t-macosx_14_0_x86_64.whl
- numpy-2.2.0-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- numpy-2.2.0-cp313-cp313t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- numpy-2.2.0-cp313-cp313t-win32.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈