JAVA 门面模式,正确实现?

JAVA Facade pattern, correct implementation?

我有一个客户端,它的构造函数在参数列表方面相当冗长,例如,

Class Client {
   private ServiceA _serviceA;
   private ServiceB _serviceB;
   ....
   private ServiceE _serviceE;

   public Client(ServiceA serviceA, ServiceB serviceB,...ServiceE service E) { ... }

   public doTask1(TypeA typeA) { //only serviceA, serviceB service being used... }

   public doTask2(TypeB typeB) { //only serviceD, serviceE being used ... }
}

我想在这里使用服务外观来修剪构造函数参数列表。但是,作为门面实现的核心职责,我感到很困惑。所以我用服务作为 class 变量和它们的 getters 写下了一个外观,如下所示:

Class Facade {
   private ServiceA _serviceA;
   private ServiceB _serviceB;
   ....
   private ServiceE _serviceE;

   getters () ...
}

在这种情况下,这是抽象外观的正确方法吗?如果不是,重构客户端的正确方法是什么 class?

Facedes 有一个完全不同的意图:创建它们是为了封装和隐藏 classes 的底层结构和行为。举个汽车的例子。它由许多组件组成:on-board 计算机、燃油泵、发动机等。如果你想启动它,只需按启动按钮:

class FuelPump {
  private boolean pumpTurnedOn;
  public FuelPump() {
    pumpTunrnedOn=false;
  }
  public boolean isPumpTunredOn() {
    return pumpTurnedOn;
  }
  public void setPumpTurnedOn (boolean newState) {
    pumpTurndeOn=newState;
    if (newState) {
      System.out.println ("fuel pump now is on");
    } else {
      System.out.println ("fuel pump now is turned off");
    }
  }
}

class Engine {
  private boolean engineStarted;
  public Engine() {
    engineStarted=false;
  }
  public boolean isEngineStarted() {
    return engineStarted;
  }
  public void setEngineStarted (boolean newState) {
    engineStarted=newState;
    if (newState) {
      System.out.println("engine now is on");
    } else {
      System.out.println("engine now is turned off");
    }
  }
}

// this is the Car facade:
class Car {
  private FuelPump fuelPump;
  private Engine engine;
  // + other components of Car
  public Car () {
    fuelPump = new FuelPump();
    engine = new Engine();
  }
  public void startCar() {
    fuelPump.setPumpTurnedOn(true);
    engine.setEngineStarted(true);
    // + other methods of start procedures with other components
    System.out.println("Your car has been startded");
  }
  public void stopCar() {
    engine.setEngineStarted(false);
    fuelPump.setPumpTurnedOn(false);
    // + other methods on other components for shutting down car
  }
}

客户端代码片段:

Car car=new Car();
car.startCar();
// later on
car.stopCar();

如您所见,客户对启动汽车的底层组件一无所知。它只需要使用 startCar() 方法,Car facade 会完成剩下的工作。 Facade 是一种 结构化 模式。 如果您有许多构造函数参数并想减少它们,请使用其中一种创建模式。如果您有必填和非必填字段,我建议使用 builder 模式。 例如,您的强制构造函数参数是 Service_A 和 Service_B 并且 Service_C 到 Service_E 不是必需的。 那么您的 ClientBuilder class 应该是这样的:

class ClientBuilder{
  private static Service_A serviceA;  // required
  private static Service_B serviceB;  // required
  private static Service_C serviceC;
  private static Service_D serviceD;
  private static Service_E serviceE;

  // since this builder is singleton
  private static ClientBuilder builderInstance = new ClientBuilder();
  private ClientBuilder () {};

  public static ClientBuilder getBuilderInstance (Service_A service_A, Service_B service_B){
    serviceA = service_A;
    serviceB = service_B;
    serviceC = null; 
    serviceD = null;
    serviceE = null;
    return builderInstance;
  }

  public static ClientBuilder addServiceC (Service_C service_C) {
    serviceC = service_C;
    return builderInstance; 
  }

  public static ClientBuilder addServiceD (Service_D service_D) {
    serviceC = service_D;
    return builderInstance; 
  }

  public static ClientBuilder addServiceE (Service_E service_E) {
    serviceE = service_E;
    return builderInstance; 
  }

  public static Client build(){
    return new Client (serviceA, ServiceB, ServiceC, ServiceD, ServiceE);
  }

在这种情况下,您可以仅使用强制参数实例化您的客户端 class。最好的是不需要的参数顺序可以互换:

  Client aClient = ClientBuilder.getBuilderInstance(aServiceA, aServiceB)
        .addServiceE(aServiceE)
        .addServiceC(aServiceC)
        .build();

现在已经使用服务 A、B、C、E 创建了 aClient,而 serviceD 仍然为空。以后可以适当设置setter。吸气剂和 setters 必须在您的客户端 class 中。 简而言之,使用构建器 class,您可以仅针对必需字段减少构造函数参数的数量,并稍后使用 setters 设置可选字段。 您可以在 Gang of Four book or if you are a serious Java fun I suggest Head First's Design Patterns 书中阅读更多详细信息。 希望能帮到你,再见!