首页 > 解决方案 > 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?

标签: classoopuml

解决方案


这可能会有所帮助: https ://softwareengineering.stackexchange.com/questions/316678/law-of-demeter-and-its-applicability

它谈到了得墨忒耳法则以及为什么你通常应该避免这种类型的链接。


推荐阅读