Facade Design Pattern in Java – Java中的Facade设计模式

最后修改: 2018年 4月 15日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this quick tutorial, we’re going to take a look at one of the structural design patterns: the Facade.

在这个快速教程中,我们要看一下结构化设计模式之一:Facade

First, we’ll give an overview of the pattern, list its benefits and describe what problems it solves.

首先,我们将对该模式进行概述,列出它的好处并描述它解决了哪些问题。

Then, we’ll apply the facade pattern to an existing, practical problem with Java.

然后,我们将把facade模式应用于一个现有的、实际的Java问题。

2. What Is a Facade?

2.什么是门面?

Simply put, a facade encapsulates a complex subsystem behind a simple interface. It hides much of the complexity and makes the subsystem easy to use.

简单地说,门面将一个复杂的子系统封装在一个简单的接口后面。它隐藏了大部分的复杂性,使子系统易于使用。

Also, if we need to use the complex subsystem directly, we still can do that; we aren’t forced to use the facade all the time.

另外,如果我们需要直接使用复杂的子系统,我们仍然可以这样做;我们并没有被迫一直使用门面。

Besides a much simpler interface, there’s one more benefit of using this design pattern. It decouples a client implementation from the complex subsystem. Thanks to this, we can make changes to the existing subsystem and don’t affect a client.

除了更简单的接口,使用这种设计模式还有一个好处。它将客户端的实现与复杂的子系统解耦。因此,我们可以对现有的子系统进行修改,而不影响客户端。

Let’s see the facade in action.

让我们看看这个门面的作用。

3. Example

3.例子

Let’s say that we want to start a car. The following diagram represents the legacy system, which allows us to do so:

比方说,我们想启动一辆汽车。下图表示遗留系统,它允许我们这样做。

facade class diagram

 

As you can see, it can be quite complex and does require some effort to start the engine correctly:

正如你所看到的,它可能是相当复杂的,而且确实需要一些努力才能正确启动发动机

airFlowController.takeAir()
fuelInjector.on()
fuelInjector.inject()
starter.start()
coolingController.setTemperatureUpperLimit(DEFAULT_COOLING_TEMP)
coolingController.run()
catalyticConverter.on()

Similarly, stopping the engine also requires quite a few steps:

同样地,停止发动机也需要相当多的步骤。

fuelInjector.off()
catalyticConverter.off()
coolingController.cool(MAX_ALLOWED_TEMP)
coolingController.stop()
airFlowController.off()

A facade is just what we need here. We’ll hide all the complexity in two methods: startEngine() and stopEngine().

一个门面正是我们在这里需要的。我们将在两个方法中隐藏所有的复杂性。startEngine()stopEngine()

Let’s see how we can implement it:

让我们看看如何实现它。

public class CarEngineFacade {
    private static int DEFAULT_COOLING_TEMP = 90;
    private static int MAX_ALLOWED_TEMP = 50;
    private FuelInjector fuelInjector = new FuelInjector();
    private AirFlowController airFlowController = new AirFlowController();
    private Starter starter = new Starter();
    private CoolingController coolingController = new CoolingController();
    private CatalyticConverter catalyticConverter = new CatalyticConverter();

    public void startEngine() {
        fuelInjector.on();
        airFlowController.takeAir();
        fuelInjector.on();
        fuelInjector.inject();
        starter.start();
        coolingController.setTemperatureUpperLimit(DEFAULT_COOLING_TEMP);
        coolingController.run();
        catalyticConverter.on();
    }

    public void stopEngine() {
        fuelInjector.off();
        catalyticConverter.off();
        coolingController.cool(MAX_ALLOWED_TEMP);
        coolingController.stop();
        airFlowController.off();
    }

Now, to start and stop a car, we need only 2 lines of code, instead of 13:

现在,要启动和停止一辆汽车,我们只需要2行代码,而不是13行:

facade.startEngine();
// ...
facade.stopEngine();

4. Drawbacks

4.缺点

The facade pattern doesn’t force us to unwanted tradeoffs, because it only adds additional layers of abstraction.

Facade模式并没有强迫我们进行不必要的权衡,因为它只是增加了额外的抽象层。

Sometimes the pattern can be overused in simple scenarios, which will lead to redundant implementations.

有时该模式在简单的场景中会被过度使用,这将导致多余的实现。

5. Conclusion

5.总结

In this article, we’ve explained the facade pattern and demonstrated how to implement it atop of an existing system.

在这篇文章中,我们解释了facade模式,并演示了如何在现有系统上实现它。

The implementation of these examples can be found over on GitHub.

这些例子的实现可以在GitHub上找到over