设计模式:减少java中两种几乎相同的方法的重复代码

发布于 2024-12-11 09:41:14 字数 427 浏览 1 评论 0原文

我有多种车辆,如卡车、轿车、货车、摩托车。它们之间的共同点是它们都会移动,因此我将其委托给包含以下方法的移动类:

getSpeed()
setSpeed()
getDestination()
setDestination()
..
..
..
move()

有些车辆的移动方式与其他车辆不同,因此我为其制作了一个工厂设计模式。因此,如果它是一辆汽车(卡车、轿车、货车),它将委托给 MoveCarImpl 类,或者如果它是一辆摩托车,它将委托给 MoveBikeImpl 类。我的问题是。自行车和汽车具有几乎完全相同的 move() 算法。唯一的区别是自行车算法在 move() 方法末尾调用 1 个附加方法,而汽车的 move() 算法则不会。所以现在我在 MoveCarImpl 类和 MoveBikeImpl 类中有很多重复的代码。关于如何处理这个问题有什么想法吗?

I have several classes of vehicles such as truck, sedan, van, motorcycle. The common thing between them is that they all move, so I delegate it to a move class which contains these methods:

getSpeed()
setSpeed()
getDestination()
setDestination()
..
..
..
move()

Some vehicles move differently then others so I make a factory design pattern for it. So if it is it a car(truck,sedan, van) it would delegate to the MoveCarImpl class or if it is a motorcycle, it would delegate to the MoveBikeImpl class. My issue is. the bike and car has almost exactly the same move() algorithm. The only difference is that the bike algorithm calls 1 additional method at the end of move() method while car's move() algorithm doesn't. So now I have a lot of duplication of code in the MoveCarImpl class and MoveBikeImpl class. Any ideas on how to deal with this?

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

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

发布评论

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

评论(6

深府石板幽径 2024-12-18 09:41:14

您可以使用自行车移动实现类来扩展汽车移动实现类并覆盖移动方法。

You can extend your car move implementation class with the bike move implementation class and override the move method.

情未る 2024-12-18 09:41:14

在基类 MoveImpl 中移动 MoveCarImplMoveBikeImpl 之间的公共代码。从中继承 Move car 和 Move bike,并为每个方法调用基类方法,然后执行一些额外的特定方法调用。

Move the common code between MoveCarImpl and MoveBikeImpl in a base class MoveImpl. Inherit both the Move car and Move bike from it and for each method call the base class method and then do some additional specific method call.

绅刃 2024-12-18 09:41:14

您可以拥有一个 util 类,并在其中包含一个方法,其中包含汽车和自行车使用的通用代码,但在 moveBikeImpl 中,在此 util 类的方法之后调用该额外方法。

You can have an util class and have a method in it with the common code used by both car and bike, but in the moveBikeImpl call that extra method after this util class's method.

自由如风 2024-12-18 09:41:14

你可以创建一个Move类,并在这个类中编写你的move方法。
从移动类扩展两个类 CarMove 和 Bike move
CarMove会继承该方法,不需要做任何事情,BikeMove类可以重写move()方法,重写的方法可以调用超级move方法,然后调用endMove方法(),因为最后需要调用endMove()。

您将可以灵活地为卡车、轿车等添加另一个可以从 Move 类继承的 Move 类。

u can make a Move class, and write ur move method in this class.
extend two classes CarMove and Bike move from move class
CarMove will inherit the method, no need to do anything, BikeMove class can override method of move(), overrided method can call super move method and then endMove method(), since endMove() needs to be called at the end.

and u will have the flexibility to add another Move classes for truck, sedan e.t.c, that can inherit from Move class.

我的痛♀有谁懂 2024-12-18 09:41:14

尽管我尊重您使用委托而不是继承的决定,但我发现拥有两个彼此并行的类层次结构是一种代码味道。如果您发现向系统添加新型车辆需要将两个(或更多)类添加到独立的继承层次结构中,那么可能是时候重新考虑委托而不是继承的决定了。

Much as I respect your decision to use delegation over inheritance, I find that having two class hierarchies that parallel each other is a code smell. If you find that adding a new type of vehicle to the system requires that you add two (or more) classes to independent inheritance hierarchies, then it may be time to reconsider the decision to delegate rather than inherit.

小苏打饼 2024-12-18 09:41:14

另一种选择是 Move 类包含返回 Vehicle 类的指针。 (您可以将它传递到构造函数中,并将其作为成员变量保存。您可能会发现泛型很有用。)Move 类可以回调 Vehicle 类以实现某些特定于车辆的行为 - 将 Vehicle 类视为一种策略模式。

Another option is for the Move class to contain a pointer back to the Vehicle class. (You'd pass it into the constructor, and hold it as a member variable. You would probably find Generics useful.) The Move class can call back to the Vehicle class for some vehicle-specific behavior -- treating the Vehicle class as a kind of Strategy pattern.

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