class - Having duplicate methods in association/composition relationship
问题描述
Assume the class Car
contains the class Engine
. Now, Engine
has some methods, namely start()
,stop()
etc. Now, I give the Car
class to a Driver
. I now want the Driver
to drive()
the Car
. Engine
already has a start()
method. So should Car
and Driver
also have a startCar()
method? I mean Driver
's startCar()
method will internally call Car
's startCar()
method, which will finally call Engine
's start()
method. So is it a good practice to do so?
Other way to do so is I will do Driver.getCar().getEngine().start()
. Which one is better in terms of code maintainability and OOP?
解决方案
这可能会有所帮助: https ://softwareengineering.stackexchange.com/questions/316678/law-of-demeter-and-its-applicability
它谈到了得墨忒耳法则以及为什么你通常应该避免这种类型的链接。
推荐阅读
- flutter - 收到 fcm onBackgroundMessage 后颤振打开应用程序
- json - 即使 application/json 标头存在,lambda 函数中的 event.body 也不是对象
- python - 迭代字典键和值
- python - pygame中的碰撞与pytmx对象不起作用
- javascript - 如何在javascript中让char不被somechar包围
- python-iris - 根据坐标而不是索引更改虹膜立方体中的值
- apache-spark - 如何确保输出数据帧是动态分区的,使得每个分区大约为 128mb?
- flutter - 扑。根据子画布绘图设置父级的高度
- spring-boot - 如何根据文件类型选择要运行的春季批处理作业
- typescript - Nexus Prisma - 如何在全球范围内使用 crud 处理 createdAt 和 updatedAt?