JAVA Facade模式,正确实现?

时间:2018-03-03 18:47:20

标签: java oop service refactoring facade

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

const myParse = (obj, reviver) => {
  if (typeof reviver !== 'function') {
    return JSON.parse(obj)
  }
  const undefs = []
  const rev = (k, v) => {
    const val = reviver(k, v)
    if (typeof val === 'undefined') {
      undefs.push(k)
    }
    return val;
  }
  const ret = JSON.parse(obj, rev)
  undefs.forEach(k => ret[k] = undefined)
  return ret
}

const result = myParse(
  JSON.stringify({a: 1, b:2}), 
  (k, v) => v===2 ? undefined : v
)

console.log(result)

我想在这里使用服务外观来修剪构造函数参数列表。但是,我很困惑作为Facade实现的核心职责。所以我写了一个关于服务作为类变量及其getter的外观,如下所示:

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 ... }
}

在这种情况下,这是抽象立面的正确方法吗?如果没有,那么重构Client类的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

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

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是一种结构模式。 如果你有许多构造函数参数并想要重新使用它们,请使用其中一个创建模式。如果您有强制性和非强制性字段,我建议使用构建器模式。 例如,您的必需构造函数参数是Service_A和Service_B,而Service_C不需要Service_C。 那么你的ClientBuilder类应该是这样的:

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);
  }

在这种情况下,您只能使用必需参数即时连接Client类。最好的事情是不需要的参数'订单可以互换:

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

现在已经使用服务A,B,C,E创建了aClient,并且serviceD保持为null。稍后您可以通过适当的setter进行设置。 getter和setter必须位于Client类中。 简而言之,使用构建器类,您可以减少强制的构造函数参数的数量,稍后使用setter设置可选字段。 您可以在Gang of Four book中阅读更多详细信息,或者如果您是一本严肃的Java乐趣,我建议Head First's Design Patterns预订。 希望我能帮助你,再见!