• facade(门面模式或外观模式)


    一、介绍

    外观模式:为子系统的一组接口提供一个一致的界面,此模式提供了一个高层级的接口,这个接口是的这个子系统更加易用。

    Facade pattern provides a simplified interface to a complex subsystem.

    大话设计模式》中的例子之韭菜的故事

    应用门面模式之前,客户需要自己去买股票、债券等各种理财产品,相对来说比比较复杂,需要了解的知识较多。

    在用于门面模式后,客户只需要跟基金经理打交道,不需要去关注股票等产品。此时基金经理就相当于定义中一个更高层级的接口,通过基金经理使得复杂的理财操作变得简单。

    二、门面模式实践

    例子来自java-design-patterns.com

    通过门面操作金矿工人,门面定义了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
    	}
    }
    • 3名矿工
    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 List workers;
    
        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.
  • 相关阅读:
    学习记忆——方法篇——联想法+记忆宫殿+数字编码
    str.c_str() 补充C中没有string类型的问题
    关于vue.extend的理解应用
    LeetCode·每日一题·662.二叉树最大宽度·递归·迭代
    Java Runtime 类详解
    加上boot程序,FreeRTOS就跑不起来了
    函数式编程:一等对象、作用域和高阶函数的综合指南
    SpringBoot中@Scheduled定时任务重复执行、输出两次日志的问题
    【从零开始学习深度学习】2. 深度学习框架Pytorch如何自动求梯度(gradient)
    如何优雅的创建线程
  • 原文地址:https://blog.csdn.net/qq_42008471/article/details/126899406