K
kk_oop
Hi. I just attended a design review where something funky was
presented. I recommended a different approach but was unable to
convince others that my approach was worth doing. I wanted to get some
feedback from this group on this problem.
We have three classes, A, B and C. Class A is abstract. B extends A
and provides implementation for all of A's abstract methods. Part of
this implementation includes the definition of three private methods in
B, let's call them do1, do2 and do3. These methods can be implemented
in two different ways based on some criteria. So the problem at hand
was how to handle the two different implementations.
The designer did this:
B extends A and adds private methods do1, do2 and do3.
C extends B and overrides do1, do2 and do3 with its own private
implementations. So some clients will use B polymorphicaly through A,
and other clients will use C polymorphically through A.
This works, but it seemed to couple parts of C to B unnecessarily.
Essentially, if I change do1, do2 or do3 implementation in B, C will be
impacted since it extends B. That doesn't seem like a good thing,
since C doesn't really care at all about B's implementation of these
methods.
I recommended using the template method pattern instead. Basically, I
said to leave do1, do2 and do3 abstract in B. Then make a C1 and a C2.
Each would extend B and provide its own implementation of do1, do2 and
do3.
The designer's response was that there would never be more than these
two variations of do1/do2/do3, so the template method pattern's
scalability benefit was a non-issue. Also, these three methods were
pretty simple, so decoupling the C and B implementations was not a big
factor. What was a big factor was adding the extra class (C1 and C2
instead of just C), and making the code a bit more complex from an OO
perspective--which would make it harder for folks new to OO to
understand (we have many folks on the project who are new to OO).
I can see his point, but it still just doesn't sit right.
Any thoughts for or against either approach?
Thanks for any input!
Ken
presented. I recommended a different approach but was unable to
convince others that my approach was worth doing. I wanted to get some
feedback from this group on this problem.
We have three classes, A, B and C. Class A is abstract. B extends A
and provides implementation for all of A's abstract methods. Part of
this implementation includes the definition of three private methods in
B, let's call them do1, do2 and do3. These methods can be implemented
in two different ways based on some criteria. So the problem at hand
was how to handle the two different implementations.
The designer did this:
B extends A and adds private methods do1, do2 and do3.
C extends B and overrides do1, do2 and do3 with its own private
implementations. So some clients will use B polymorphicaly through A,
and other clients will use C polymorphically through A.
This works, but it seemed to couple parts of C to B unnecessarily.
Essentially, if I change do1, do2 or do3 implementation in B, C will be
impacted since it extends B. That doesn't seem like a good thing,
since C doesn't really care at all about B's implementation of these
methods.
I recommended using the template method pattern instead. Basically, I
said to leave do1, do2 and do3 abstract in B. Then make a C1 and a C2.
Each would extend B and provide its own implementation of do1, do2 and
do3.
The designer's response was that there would never be more than these
two variations of do1/do2/do3, so the template method pattern's
scalability benefit was a non-issue. Also, these three methods were
pretty simple, so decoupling the C and B implementations was not a big
factor. What was a big factor was adding the extra class (C1 and C2
instead of just C), and making the code a bit more complex from an OO
perspective--which would make it harder for folks new to OO to
understand (we have many folks on the project who are new to OO).
I can see his point, but it still just doesn't sit right.
Any thoughts for or against either approach?
Thanks for any input!
Ken