gpt4 book ai didi

JAVA Facade模式,正确实现吗?

转载 作者:行者123 更新时间:2023-12-02 11:34:00 28 4
gpt4 key购买 nike

我有一个像构造函数这样的客户端,它的参数列表相当长,例如,

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

我想在这里使用服务外观来修剪构造函数参数列表。然而,我对外观实现的核心职责感到非常困惑。因此,我写下了一个外观,其中服务作为类变量及其 getter ,如下所示:

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

getters () ...
}

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

最佳答案

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 外观将完成其余的工作。外观是一种结构模式。如果您有许多构造函数参数并希望减少它们,请使用创建模式之一。如果您有必填字段和非必填字段,我建议使用构建器模式。例如,您的强制构造函数参数是 Service_A 和 Service_B,而 Service_C 到 Service_E 不是必需的。那么你的 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 保持为空。稍后您可以通过适当的 setter 来设置它。 getter 和 setter 必须位于您的 Client 类中。简而言之,使用构建器类,您可以仅减少强制参数的构造函数参数数量,并在稍后使用 setter 设置可选字段。您可以在 Gang of Four book 中阅读更多详细信息或者如果你是一个认真的 Java 爱好者,我建议 Head First's Design Patterns书。希望能帮到你,再见!

关于JAVA Facade模式,正确实现吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49087878/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com