我是靠谱客的博主 明理枫叶,最近开发中收集的这篇文章主要介绍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<DwarvenMineWorker> 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<DwarvenMineWorker> 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.

最后

以上就是明理枫叶为你收集整理的facade(门面模式或外观模式)的全部内容,希望文章能够帮你解决facade(门面模式或外观模式)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(182)

评论列表共有 0 条评论

立即
投稿
返回
顶部