OOP best practice: Employee.GetCars() vs Cars.GetB

2019-04-08 01:26发布

Given the classes Company, Employee, and Car what is the preferred practice for methods to retrieve Cars associated with Company or Employee?

Employee.GetCars(params...)
Company.GetCars(params...)

Or:

Cars.GetByEmployee(params...)
Cars.GetByCompany(params...)

The first approach is the one I have generally used and always seemed the most intuitive to me. But after seeing a large code-base that used the second approach I have to admit that it's growing on me. The two things I really like about the second approach are:

  • It groups all Car related code together into one file, making the code more modular and easier to maintain.
  • There is an intuitive logic to having any method with a return value of Car (or more like List<Car> in this case) grouped into the Car class.

Is there a best practice that covers this?

8条回答
▲ chillily
2楼-- · 2019-04-08 01:59

There is no best way. It depends on what your application needs to do. Two applications that use the same data can have completely different object models, depending on their use cases.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-08 01:59

Run this problem through Law of Demeter logic processing. Very helpful, right! hehe

The question is presented in a way that will always have a coupling between Employee and Car classes. If you can change car.GetByEmployee(...) to car.GetByDriversLicenseNumber(...) (or something similiar) then you would decouple the two classes.

The best approach would be to reduce the number of objects that are coupled toghether. So, it all depends on how the next level object in the chain will the Car.

I don't think there's one right awswer to this question, it all is about the current situation.

查看更多
男人必须洒脱
4楼-- · 2019-04-08 02:01

Both approaches seem a little off to me. Why should the Employee class know about the Car class? Why should the Car class know about Employee? Neither class needs the other to function, so coupling them is unnecessary. I would just store a dictionary somewhere that mapped employees to a collection of cars and another that mapped companies to a collection of cars.

查看更多
太酷不给撩
5楼-- · 2019-04-08 02:02

I would use the first approach in the entity classes. There should be no params to these methods as they only return all associations. The second approach which involves some simple business logic should be placed in a helper class or maybe the CarDAO, if you have one.

查看更多
虎瘦雄心在
6楼-- · 2019-04-08 02:06

An employee has a car (or some cars, for that matter), so every employee naturally knows which cars (s)he uses. But does the car know or care who "owns" it? I'd say no. It may know who is driving it, but that's a different question.

Either the car does know which employee owns it (which feels wrong, it's a weird has-a relationship), or it has to search all employeed to find itself, which is even worse (illogical, dog slow, everything but loose coupling).

查看更多
可以哭但决不认输i
7楼-- · 2019-04-08 02:22

I think there isn't a single solution. It depends how you argue. If it is the responsible of the cars to know by whom they are owned, I would used the second approach. But if it is the responsibilty of the employee to know which cars he has, then I would use the first approach.

However I personally would prefer the first approach because it seems to be easier to implemented.

查看更多
登录 后发表回答