在 Java 中实现一个类
我正在尝试编写一个驾驶模拟程序,我需要一些设计方面的帮助。我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两种方法都是有效的,是否需要对车辆进行显式抽象取决于您的需求。
SedanImpl
、TruckImpl
等可能共享一些车辆常见的功能,但不包括人类/动物:正如您在 Java 中标记的那样,常见的
abstract
类可能也很有用:这样
TruckImpl
可以继承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:As you tagged this in Java common
abstract
classes may be useful as well:So that
TruckImpl
can inherit fromAbstractVehicle
:合理的东西:
但是,这仍然取决于要求。没有像你这样明确的要求,我想这很简单。
Something reasonable:
However, it's still depending on the requirements. W/o clear requirements as yours, I guess this is simple enough.
我认为车辆和人们是个好主意。我会为此使用抽象类。
还要考虑行为。校车与摩托车有一些不同的行为,您可以使用接口来实现这些行为。
请查看这里了解更多想法。
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.