在 Java 中实现一个类

发布于 2024-12-08 15:19:10 字数 978 浏览 1 评论 0原文

我正在尝试编写一个驾驶模拟程序,我需要一些设计方面的帮助。我有一个名为 RoadObjects 的界面,目前包含车辆和哺乳动物。我需要实现不同类型的汽车,例如卡车、轿车、半挂车。它们有一些共同的方法,也有一些独特的方法。稍后我会添加其他种类的交通工具和哺乳动物。我不想使用继承,我试图根据设计原则和模式(例如开闭原则和工厂)来做到这一点。现在我的问题是,我应该这样设计它:

RoadObject interface

Vehicle extends RoadObject

Mammal extends RoadObject

SedanImpl implements Vehicle

TruckImpl implements Vehicle

People implements Mammal

……

还是

我应该没有车辆和人员界面就这样做?

RoadObject interface

SedanImpl implements RoadObject

TruckImpl implements RoadObject

People implements RoadObject

新问题: 我想使用工厂模式,例如选择哪种车辆。所以我制作了一个 VechicleImpl 和 VechicleImplFactory 所以它可能看起来像这样:

RoadObject interface

Vehicle extends RoadObject

VehicleImpl implements Vehicle

Mammal extends RoadObject

SedanImpl implements Vehicle

TruckImpl implements Vehicle

People implements Mammal

这是一种糟糕的设计吗?因为VehicleImpl会有很多重复的SedanImpl、TruckImpl等方法。或者如果我想要一个创建Vehicle的工厂,那么这个工厂应该在哪里?

I'm trying to write a driving simulation program and I need some help on the design. I have a interface called RoadObjects which for now contains vehicles and mammals. I need to implement different kind of cars such as trucks, sedans, semis. they have some methods in common and some unique. Later on, I will add other kinds of vehicles and mammals. I don't want to use inheritance and I am trying to do this according to design principles and patterns such as open-closed principle and factory. Now my question is, should I design it like this:

RoadObject interface

Vehicle extends RoadObject

Mammal extends RoadObject

SedanImpl implements Vehicle

TruckImpl implements Vehicle

People implements Mammal

...

...

or should I just not have vehicle and people interface and do this?

RoadObject interface

SedanImpl implements RoadObject

TruckImpl implements RoadObject

People implements RoadObject

New question:
I want to use a factory pattern, to chose which kind of vehicles for example. So I make a VechicleImpl and VechicleImplFactory so it might look like this:

RoadObject interface

Vehicle extends RoadObject

VehicleImpl implements Vehicle

Mammal extends RoadObject

SedanImpl implements Vehicle

TruckImpl implements Vehicle

People implements Mammal

Is this kind of bad design? Because VehicleImpl will have a lot of duplicate methods of SedanImpl,TruckImpl etc. Or if I wanted to have an factory that createVehicle, where should this be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗弃M 2024-12-15 15:19:10

两种方法都是有效的,是否需要对车辆进行显式抽象取决于您的需求。 SedanImplTruckImpl 等可能共享一些车辆常见的功能,但不包括人类/动物:

public interface Vehcile extends RoadObject
    getTires()
    getEngine()

正如您在 Java 中标记的那样,常见的 abstract 类可能也很有用:

public abstract class AbstractVehicle implements Vehicle {
    private Integer tires = 4;

    public Integer getTires() { return this.tires; }

    /* ... */
}

这样 TruckImpl 可以继承 AbstractVehicle

public class TruckImpl extends AbstractVehicle {
    /* ... */
}

Both approaches are valid, whether you need an explicit abstraction for vehicles depends on your requirements. SedanImpl, TruckImpl etc. may share some features common to vehicles but not to humen/animals:

public interface Vehcile extends RoadObject
    getTires()
    getEngine()

As you tagged this in Java common abstract classes may be useful as well:

public abstract class AbstractVehicle implements Vehicle {
    private Integer tires = 4;

    public Integer getTires() { return this.tires; }

    /* ... */
}

So that TruckImpl can inherit from AbstractVehicle:

public class TruckImpl extends AbstractVehicle {
    /* ... */
}
要走就滚别墨迹 2024-12-15 15:19:10

合理的东西:

- public interface IRoadObject

- public class People implements IRoadObject

- public class Vehicle

- public class Truck extends Vehicle implements IRoadObject

- public class Sedan extends Vehicle implements IRoadObject

但是,这仍然取决于要求。没有像你这样明确的要求,我想这很简单。

Something reasonable:

- public interface IRoadObject

- public class People implements IRoadObject

- public class Vehicle

- public class Truck extends Vehicle implements IRoadObject

- public class Sedan extends Vehicle implements IRoadObject

However, it's still depending on the requirements. W/o clear requirements as yours, I guess this is simple enough.

玩套路吗 2024-12-15 15:19:10

我认为车辆和人们是个好主意。我会为此使用抽象类。

还要考虑行为。校车与摩托车有一些不同的行为,您可以使用接口来实现这些行为。

请查看这里了解更多想法。

I think vehicle & people is a good idea. I would use abstract classes for this.

Also think about behaviors. A school bus has some different behaviors than a motorcycle, and you can implement these with interfaces.

Look around here for some more ideas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文