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); // 关闭厨房灯光
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
23种设计模式java实现源码byhrh (164个子文件)
Composite.class 2KB
ConcreteSubject.class 2KB
Client.class 2KB
Client.class 1KB
Client.class 1KB
ConcreteHandlerA.class 1KB
ConcreteHandlerB.class 1KB
SingletonEnum.class 1KB
Request$Type.class 1KB
Client.class 1KB
Client2.class 1KB
ObjectStructure.class 1KB
ConcreteAggregate.class 1KB
Client.class 1KB
Client.class 1KB
Client.class 1KB
FlyweightFactory.class 1KB
Client.class 1KB
Client.class 1KB
Leaf.class 1KB
Client.class 1KB
Client.class 1KB
ConcreteBuilder.class 1KB
Originator.class 1018B
ConcreteIterator.class 1013B
Client.class 1011B
Client1.class 1000B
ConcreteVisitor.class 996B
ConcreteElementA.class 988B
ConcreteElementB.class 988B
ConcretePrototype.class 980B
LivingRoomLight.class 976B
KitchenLight.class 962B
ConcreteStateA.class 951B
ConcreteStateB.class 951B
Client.class 947B
Product.class 940B
Facade.class 937B
ConcreteFlyweight.class 923B
ConcreteObserver.class 894B
ConcreteDecoratorA.class 893B
ConcreteDecoratorB.class 893B
SingletonLazy4$SingletonHolder.class 883B
Client.class 877B
RefinedAbstraction.class 876B
SingletonLazy4.class 874B
Client.class 874B
Proxy.class 818B
Client.class 800B
Request.class 800B
ConcreteFactory2.class 787B
ConcreteFactory1.class 787B
SimpleLightMediator.class 782B
ConcreteAlgorithmB.class 780B
ConcreteAlgorithmA.class 780B
MultiplyExpression.class 763B
Client.class 761B
AddExpression.class 753B
Context.class 743B
Algorithm.class 741B
Context.class 735B
Invoker.class 709B
ConcreteCommand.class 708B
Director.class 695B
Caretaker.class 681B
Client.class 663B
ConcreteImplementorA.class 653B
ConcreteImplementorB.class 653B
Decorator.class 652B
ConcreteComponent.class 644B
ConcreteStrategyA.class 640B
ConcreteStrategyB.class 640B
Handler.class 636B
SingletonLazy3.class 630B
ConcreteProductB2.class 616B
ConcreteProductB1.class 616B
ConcreteProductA2.class 616B
ConcreteProductA1.class 616B
RealSubject.class 616B
ConcreteProductB.class 612B
ConcreteProductA.class 612B
Client.class 603B
Client.class 597B
ConcreteFactoryB.class 580B
ConcreteFactoryA.class 580B
Adaptee.class 576B
SingletonHungry2.class 564B
SingletonHungry1.class 560B
Receiver.class 557B
SubSystemB.class 555B
SubSystemA.class 555B
SubSystemC.class 555B
SingletonLazy1.class 541B
SingletonLazy2.class 541B
Main.class 536B
NumberExpression.class 529B
Abstraction.class 519B
Memento.class 514B
Adapter.class 513B
Expression.class 365B
共 164 条
- 1
- 2
资源评论
hrhcode
- 粉丝: 89
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于javaweb的网上拍卖系统,采用Spring + SpringMvc+Mysql + Hibernate+ JSP技术
- polygon-mumbai
- Chrome代理 switchyOmega
- GVC-全球价值链参与地位指数,基于ICIO表,(Wang等 2017a)计算方法
- 易语言ADS指纹浏览器管理工具
- 易语言奇易模块5.3.6
- cad定制家具平面图工具-(FG)门板覆盖柜体
- asp.net 原生js代码及HTML实现多文件分片上传功能(自定义上传文件大小、文件上传类型)
- whl@pip install pyaudio ERROR: Failed building wheel for pyaudio
- Constantsfd密钥和权限集合.kt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功