外观模式:为子系统的一组接口提供一个一致的界面,此模式提供了一个高层级的接口,这个接口是的这个子系统更加易用。
Facade pattern provides a simplified interface to a complex subsystem.
《大话设计模式》中的例子之韭菜的故事
应用门面模式之前,客户需要自己去买股票、债券等各种理财产品,相对来说比比较复杂,需要了解的知识较多。
在用于门面模式后,客户只需要跟基金经理打交道,不需要去关注股票等产品。此时基金经理就相当于定义中一个更高层级的接口,通过基金经理使得复杂的理财操作变得简单。
通过门面操作金矿工人,门面定义了startNewDay、digOutGold和endDay,通过门面DwarvenGoldmineFacade操作3个金矿工人挖矿。
package com.keller.design.facade; import lombok.extern.slf4j.Slf4j; import java.util.Arrays; @Slf4j public abstract class DwarvenMineWorker { public void goToSleep() { log.info("{} goes to sleep.", name()); } public void wakeUp() { log.info("{} wakes up.", name()); } public void goHome() { log.info("{} goes home.", name()); } public void goToMine() { log.info("{} goes to the mine.", name()); } private void action(Action action) { switch (action) { case GO_TO_SLEEP: goToSleep(); break; case WAKE_UP: wakeUp(); break; case GO_HOME: goHome(); break; case GO_TO_MINE: goToMine(); break; case WORK: work(); break; default: log.info("Undefined action"); break; } } public void action(Action... actions) { Arrays.stream(actions).forEach(this::action); } public abstract void work(); public abstract String name(); enum Action { GO_TO_SLEEP, WAKE_UP, GO_HOME, GO_TO_MINE, WORK } }
package com.keller.design.facade; import lombok.extern.slf4j.Slf4j; @Slf4j public class DwarvenCartOperator extends DwarvenMineWorker { @Override public void work() { log.info("{} moves gold chunks out of the mine.", name()); } @Override public String name() { return "Dwarf cart operator"; } }
package com.keller.design.facade; import lombok.extern.slf4j.Slf4j; @Slf4j public class DwarvenGoldDigger extends DwarvenMineWorker { @Override public void work() { log.info("{} digs for gold.", name()); } @Override public String name() { return "Dwarf gold digger"; } }
package com.keller.design.facade; import lombok.extern.slf4j.Slf4j; @Slf4j public class DwarvenTunnelDigger extends DwarvenMineWorker { @Override public void work() { log.info("{} creates another promising tunnel.", name()); } @Override public String name() { return "Dwarven tunnel digger"; } }
package com.keller.design.facade; import java.util.Arrays; import java.util.Collection; import java.util.List; public class DwarvenGoldmineFacade { private final Listworkers; public DwarvenGoldmineFacade() { workers = Arrays.asList( new DwarvenGoldDigger(), new DwarvenCartOperator(), new DwarvenTunnelDigger()); } public void startNewDay() { makeActions(workers, DwarvenMineWorker.Action.WAKE_UP, DwarvenMineWorker.Action.GO_TO_MINE); } public void digOutGold() { makeActions(workers, DwarvenMineWorker.Action.WORK); } public void endDay() { makeActions(workers, DwarvenMineWorker.Action.GO_HOME, DwarvenMineWorker.Action.GO_TO_SLEEP); } public static void makeActions(Collection workers, DwarvenMineWorker.Action... actions) { workers.forEach(worker -> worker.action(actions)); } }
package com.keller.design.facade; public class Test { public static void main(String[] args) { DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade(); facade.startNewDay(); facade.digOutGold(); facade.endDay(); } }
23:29:47.291 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarf gold digger wakes up. 23:29:47.300 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarf gold digger goes to the mine. 23:29:47.301 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarf cart operator wakes up. 23:29:47.301 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarf cart operator goes to the mine. 23:29:47.302 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarven tunnel digger wakes up. 23:29:47.302 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarven tunnel digger goes to the mine. 23:29:47.302 [main] INFO c.k.design.facade.DwarvenGoldDigger - Dwarf gold digger digs for gold. 23:29:47.303 [main] INFO c.k.d.facade.DwarvenCartOperator - Dwarf cart operator moves gold chunks out of the mine. 23:29:47.303 [main] INFO c.k.d.facade.DwarvenTunnelDigger - Dwarven tunnel digger creates another promising tunnel. 23:29:47.304 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarf gold digger goes home. 23:29:47.304 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarf gold digger goes to sleep. 23:29:47.304 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarf cart operator goes home. 23:29:47.305 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarf cart operator goes to sleep. 23:29:47.305 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarven tunnel digger goes home. 23:29:47.306 [main] INFO c.k.design.facade.DwarvenMineWorker - Dwarven tunnel digger goes to sleep.