gpt4 book ai didi

java - 在相关接口(interface)之间分配职责

转载 作者:行者123 更新时间:2023-12-02 11:08:16 25 4
gpt4 key购买 nike

我有两个相关的接口(interface),但我不确定如何分配一些职责(方法)。

假设一个接口(interface)是 Truck,另一个接口(interface)是 ParkingLot。

Truck 可以有不同的实现,例如 DumpTruck、BoxTruck、FireTruck...,而 ParkingLot 可以有不同的实现,例如 TruckParkingLot、CarParkingLot、MotorcycleParkingLot...

我有一个程序基本上是一个 TruckParkingLot,它包含以下内容:

 private Map<String, Truck> trucks = new ConcurentHashMap();

public void registerTruck(String plate, Truck truck) {
// Code to put a new Truck in the ParkingLot
}

public void removeTruck(String plate) {
// Code to remove a Truck from the ParkingLot
}

public Truck getTruckByPlate(String plate) {
// Code to get Truck with key (plate) from the Map trucks
}

然后是 Truck 接口(interface),它有几个实现。每个实现都彼此不同,因为它们具有不同的实例变量。

我还想实现另外两种方法:更新和打印。前者更新所选卡车的实例变量;后者打印所选卡车的实例变量。

我的疑问是这些方法应该在 ParkingLot 接口(interface)中还是在 Truck 接口(interface)中。

一方面,我觉得这些方法应该位于 ParkingLot 接口(interface)中,因此客户端只需访问 TruckParkingLot y 类即可获取所需的所有信息并对其进行管理。此外,使用 Reflect,您可以轻松管理类的任何实例(包括私有(private)实例),而无需知道给定对象有多少个实例变量或哪些实例变量。

另一方面,通过将这两个方法放入 ParkingLot 接口(interface)中,该接口(interface)会管理并非内置于 TruckParkingLot 中的数据,而是来自给定卡车的实例变量,这违反了责任驱动的设计原则(私有(private)类实例只能在它们所属的类中处理)。另外,如果 update 和 print 都转到 ParkingLot 接口(interface),那么 Truck 接口(interface)就变成空接口(interface)(Truck 不再有任何方法)。

第三,如果我在 Truck 接口(interface)中实现这两个方法,那么我将为该接口(interface)的每个实现重复代码,因为更新或打印 BoxTruck 和 DumpTruck 的实例变量的方法几乎是如果使用 getter 和 setter 完成,则相同(如果使用反射完成,则完全相同)

另一种可能的解决方案是为 Truck 创建一个抽象类并实现更新和打印方法,其余卡车仅继承该类。 (我还应该为这个抽象类创建一个接口(interface)吗?)。

总而言之,当我第一次写这篇文章时,我想到了这些选择:

A) ParkingLot 接口(interface),包含所有方法;没有方法的卡车接口(interface)(空)。

 interface ParkingLot {
public void registerVehicle(String plate, Vehicle vehicle);
public void removeVehicle(String plate);
public Vehicle getVehicleByPlate(String plate);
public void updateVehicle(Vehicle vehicle);
public void printVehicleData(Vehicle vehicle);
}

public class TruckParkingLot implements ParkingLot {
private Map<String, Truck> trucks = new ConcurentHashMap();

public void registerVehicle(String plate, Vehicle truck) {
// Code to put a new Truck in the ParkingLot
}

public void removeVehicle(String plate) {
// Code to remove a Truck from the ParkingLot
}

public Truck getVehicleByPlate(String plate) {
// Code to get Truck with key (plate) from the Map trucks
}

public Truck updateVehicle(Vehicle truck) {
// Code to update the inst vars of a Truck using reflection
}

public Truck printVehicleData(Vehicle truck) {
// Code to print the inst vars of a Truck using reflection
}
}

interface Truck extends Vehicle {
// Nothing here
}

public class DumpTruck implements Truck {
// Private instance variables
// Constructor
// Setters and getters
}

public class BoxTruck implements Truck {
// Other private instance variables
// Another constructor
// Setters and getters
}

B) ParkingLot 接口(interface)及其方法,Truck 接口(interface)及其更新和打印方法。

 interface ParkingLot {
public void registerVehicle(String plate, Vehicle vehicle);
public void removeVehicle(String plate);
public Vehicle getVehicleByPlate(String plate);
}

public class TruckParkingLot implements ParkingLot {
private Map<String, Truck> trucks = new ConcurentHashMap();

public void registerVehicle(String plate, Vehicle truck) {
// Code to put a new Truck in the ParkingLot
}

public void removeVehicle(String plate) {
// Code to remove a Truck from the ParkingLot
}

public Truck getVehicleByPlate(String plate) {
// Code to get Truck with key (plate) from the Map trucks
}
}

interface Truck extends Vehicle {
public void updateVehicle(Vehicle vehicle);
public void printVehicleData(Vehicle vehicle);
// Both methods should actually be inherited from Vehicle
// interface, but this doesn't affect the point here.
// Technically the Truck methods would still be empty.
}

public class DumpTruck implements Truck {
// Private instance variables
// Constructor
// Setters and getters

public Truck updateVehicle(Vehicle truck) {
// Code to update the inst var using setters and getters
}

public Truck printVehicleData(Vehicle truck) {
// Code to print the inst var using setters and getters
}
}

public class BoxTruck implements Truck {
// Other private instance variables
// Another constructor
// Setters and getters

public Truck updateVehicle(Vehicle truck) {
// Code to update the inst vars using setters and getters
}

public Truck printVehicleData(Vehicle truck) {
// Code to print the inst vars using setters and getters
}
}

C) ParkingLot 接口(interface)及其方法,Truck 抽象类。

interface ParkingLot {
public void registerVehicle(String plate, Vehicle vehicle);
public void removeVehicle(String plate);
public Vehicle getVehicleByPlate(String plate);
}

public class TruckParkingLot implements ParkingLot {
private Map<String, Truck> trucks = new ConcurentHashMap();

public void registerVehicle(String plate, Vehicle truck) {
// Code to put a new Truck in the ParkingLot
}

public void removeVehicle(String plate) {
// Code to remove a Truck from the ParkingLot
}

public Truck getVehicleByPlate(String plate) {
// Code to get Truck with key (plate) from the Map trucks
}
}

public abstract class Truck implements Vehicle {
// private common instance variables to all trucks
// Constructor with all common instance variables
// Setters and getters

public Truck updateVehicle(Vehicle truck) {
// Code to update the inst vars using setters and getters
}

public Truck printVehicleData(Vehicle truck) {
// Code to print the inst vars using setters and getters
}
}

public class DumpTruck implements Truck {
// Private unique instance variables to DumpTruck
// Inherit constructor from superclass
// + initialize unique instance variables for this subclass
// Setters and getters for the unique inst vars of this subclass

@override
public Truck updateVehicle(Vehicle truck) {
// inherit code from superclass
// + update the unique instance variables of this subclass
}

@override
public Truck printVehicleData(Vehicle truck) {
// inherit code from superclass
// + print the unique instance variables of this subclass
}
}

public class BoxTruck implements Truck {
// Private unique instance variables to DumpTruck
// Inherit constructor from superclass
// + initialize unique instance variables for this subclass
// Setters and getters for the unique inst vars of this subclass

@override
public Truck updateVehicle(Vehicle truck) {
// inherit code from superclass
// + update the unique instance variables of this subclass
}

@override
public Truck printVehicleData(Vehicle truck) {
// inherit code from superclass
// + print the unique instance variables of this subclass
}
}

就个人而言,我更喜欢在 ParkingLot 接口(interface)中拥有所有方法并使用反射(选项 A),将 Truck 接口(interface)留空;或使用 Truck 的抽象类(选项 C)

但是,说实话,我什么都不确定。

提前致谢。

最佳答案

我相信真正的 OOP 设计要求类型密切模仿它们所代表的现实世界对象。我假设当 update() 被调用时,您打算从数据存储中检索一些值,然后依次更新实例变量。如果这是正确的,它会使你的对象可变,这可能是糟糕的设计。

在我看来,卡车从车库出来并进入现实世界中的车库,这也是你的对象结构应该做的。您可以通过将卡车传递到车库界面来“更新”您的卡车。为了使卡车不可变,该方法会传回新卡车的实例。例如:

interface Garage {
Truck service(final Truck truck);
}

其中服务方法是您的“更新”方法,但也更接近地模仿现实世界的领域。就打印而言,我建议这将是另一个类,并且该类的构造函数将为各种类型的卡车重载,因此您可以以不同的方式单独处理它们。

interface TruckPrinter {
void print();
}
class BasicTruckPrinter implements TruckPrinter {
private String output;
BasicTruckPrinter(final DumpTruck truck) { /* convert truck to output */ }
BasicTruckPrinter(final FireTruck truck) { ... }
BasicTruckPrinter(final BoxTruck truck) { ... }

void print() {
System.out.println(output);
}
}

希望这有帮助...

关于java - 在相关接口(interface)之间分配职责,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50769362/

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