开发者

OOP software design problem [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details a开发者_如何学Gond clarify the problem by editing this post.

Closed 5 years ago.

Improve this question

There are two classes:A and B.

And I need to implement a function whoViewed,

I have two options,make it an member function of A and B.Which smells a little duplicate.

Another choice is to make it a separate class:Viewer,but it's more abstract.

If you were me,which way will you choose?

To make it more general:

what will you do when you need to get statistic information across different classes(like get the latest viewed 10 entities{which can be A or B} from database) within the scope of OOP.


I would recommend not using inheritance and instead go with composition. Create a class called ViewMonitor which will handle the shared functionality. The odds are that there is no logical way to design an inheritance structure.


Obviously duplicating the whoViewed() method in 2 places is bad, so that option is off the table. You have a third option: Create a parent class that implements whoViewed() and have A and B inherit from it. The famous Gang of Four Design Patterns book recommends favoring composition over inheritance. So this would suggest making the Viewer class you mentioned in the question. Given the limited context provided in your question, I would suggest taking the GoF's advice and going with the Viewer class.

Incidentally, if you expect the implementation of whoViewed() to be different in A and B, you could make Viewer an interface and implement the interface in A and B.


if WhoViewed in A and B has the exact same functionality, make a class, from which they can inherit.

If the implementation of the method is different for both classes, of if they already inherit from another class, use an interface for A and B.


I would not suggest to introduce inheritance here, because it is serious decision, require that both classes to be truly polymorphic in all aspects. In this case - make implementation in the third class, make this class a member of A and B, and then delegate call to the whoViewed method to this member. In a pseudo code: class C { WhoViewed(); }

Class A{ C m_C; WhoVied{ m_c.WhoViwed(); }

In the B do the same as in A.

If speaking in OOD language - replace inheritance with delegation.


I would use composition and delegate to an external object. Your problem sounds like a cross cutting concern, hence AOP could be an option for you.

Depending on the language you could use Mixins (Ruby) or Traits (Scala).


This seems more like an AOP problem and should be solved using an aspect.

If you can think of the "who Viewed" as an aspect that you can then store the latest viewed 10 entities from database.

You would then intercept the calls to your entities and store the required metadata off in an easy to locate location.


if whoViewed() implementation is same for both then I would like to have a seperate helper class or an abstract class which is inherited by both A and B. if whoviewed() implementation is ways apart write them in their respective classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜