Java 設計模式 之 調停者模式
阿新 • • 發佈:2018-06-28
out pac pub house string void imp www over
http://www.verejava.com/?id=16999137231072
package com.mediator.theory; public class TestMediator { public static void main(String[] args) { Mediator mediator=new MediatorImpl(); mediator.handle("rent"); mediator.handle("sale"); } } package com.mediator.theory; public interface Mediator { public void handle(String content); } package com.mediator.theory; public class MediatorImpl implements Mediator { private HouseOwner owner1; private HouseOwner owner2; public MediatorImpl() { owner1=new HouseRent(); owner2=new HouseSale(); } @Override public void handle(String content) { if("rent".equals(content)) { owner1.action(); } if("sale".equals(content)) { owner2.action(); } } } package com.mediator.theory; public interface HouseOwner { public void action(); } package com.mediator.theory; public class HouseRent implements HouseOwner { @Override public void action() { System.out.println("客戶來了需要租房"); } } package com.mediator.theory; public class HouseSale implements HouseOwner { @Override public void action() { System.out.println("客戶來了需要賣房"); } }
http://www.verejava.com/?id=16999137231072
Java 設計模式 之 調停者模式