设计模式之回调模式
2016-08-11 14:11
239 查看
public class Demo { public static void main(String [] args){ Server s=new Server(); Client c=new Client(); c.haveLaunch(s); } } interface Money{ public int getMoney(); } class Server { public void cookTomotoWithEgg(Money money){ if(money.getMoney()>8) System.out.println("做西红柿炒蛋"); else System.out.println("不做西红柿炒蛋"); } } class Client { public void haveLaunch(Server server){ server.cookTomotoWithEgg(new Money() { @Override public int getMoney() { // TODO Auto-generated method stub System.out.println("付款7元"); return 7; } }); server.cookTomotoWithEgg(new Money() { @Override public int getMoney() { // TODO Auto-generated method stub System.out.println("付款11元"); return 11; } }); } }
Client类的方法调用Server类的方法,而Server类的方法调用接口Money的方法,Money接口的方法在Client类的方法中匿名实现.这就造成Clent执行Server中的代码,Server执行Client中的代码,看起来像他们互相调用一样,因此叫回调模式。
相关文章推荐
- 30. 代码实例-设计模式-回调方式使用模板方法
- 设计模式之回调模式
- 剖析Node.js异步编程中的回调与代码设计模式
- Java设计模式之回调
- future相比于其他并发设计模式的优势(“回调驱动(多线程环境下)”、“消息/事件驱动(Actor模型中))
- 回调设计模式
- 设计模式_接口回调
- 回调设计模式
- GOF23设计模式之模板方法模式(方法回调)的理解与实现之经典
- 设计C++回调模型(一):设计模式
- 回调设计模式
- 回调设计模式
- 回调设计模式
- 回调设计模式
- 回调设计模式
- 回调设计模式
- C++中的回调类设计模式在后台插件开发中的应用
- 设计模式4.1--回调技术实现模板方法模式
- 回调设计模式
- 回调设计模式