Reputation:
In my application, there are entities. but some entities need implement interface. for example, I have "Course", "Lesson" entities. I have created "CourseService" , "LessonService" interfaces and "CourseServiceImpl", "LessonServiceImpl". But the problem is my "Course" and "Lesson" should implement "Payable" interface. what should I do? duplicate my entities?
Thank you,
Upvotes: 0
Views: 92
Reputation: 462
Not really. I presume your Payable
interface has method pay()
. Implement it in both your entities. If the implementations are different, you're fine. If they are the same, you could extract it into another object and encapsulate it in Course
and Lesson
. Or not - there's nothing wrong with duplicating code per se - only with code that is difficult to change and extend. We have the DRY principle - Don't Repeat Yourself. I prefer DRINK - Do Repeat If Necessary, Kay? :) (Not sure who coined it though).
Anyway, I would recommend having as little code in services as possible. You will have higher cohesion if your entities can perform their responsibilities themselves and don't expose their internal structure to some service. Martin Fowler explains it quite well. Also, this.
Upvotes: 1