views:

169

answers:

7

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.

+2  A: 

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.

Asaph
So when you need to operate on two or more classes,create a new class for that?
And if I need to get the latest viewed 10 entities from database,it's hard(impossible?) to make it an interface of `A` and `B`
Prefer composition to inheritance.
Carl Manaster
@unknown (google): To answer your first question: Not always. Depends on the situation. And to answer your other question: why do you think it's hard or impossible to create an interface for the methods of `A` and `B`? It shouldn't be difficult.
Asaph
Because essentially it's a sql problem.
@unknown (google): I can't see why that changes things from an OO perspective.
Asaph
+1  A: 

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.

Natrium
+2  A: 

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.

ChaosPandion
A: 

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.

Vinay Pandey
+1  A: 

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.

David Gruzman
+1  A: 

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).

deamon
+1  A: 

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.

Gord