• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Basic object oriented design question - where do I put this code

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, I've just started working on an existing system, and I've to make a small change.

childclass calls method in super class, and requires small bit of extra processing in middle of existing method flow.

Suggestion was to drop that bit of code into an extra method in the child class, write an empty method of that name in the super class and all will be happy.

Is this a standard approach to deal with this type of issue, or would ppl generally override the entire method and stick their new code in the middle?

Apologies in advance if this is not the right forum for this type of question.

Thanks

Jax
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the option to have a empty method (ideally should be abstarct :roll: ) and have a new method with same signuture (with duplicate code + new code) in the derived class is the better solution.
 
author
Posts: 608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One approach would be to refactor the original method in the superclass to read:
originalMethod() {
firstPart();
secondPart();
}

where firstPart and secondPart are extracted from the original code.

In the child class you override it to read:
originalMethod() {
firstPart();
middlePart();
secondPart();
}

where middlepart implements that new functionality.

A second approach would be to implement originalMethod() as shown for the child class and simply stub out middlePart() in the superclass and have the child class override that.

- Scott
 
Jax Blunt
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your rapid responses.

The originally suggested method has been applied (although it can't be abstract as the class isn't) rather than the refactoring because of the perceived effort refactoring incurs.

I think though that the refactoring would have improved maintainability in the long run - difficult to tell whether this is a long term benefit until I know the system better.

Jax
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Ambler:
...
where firstPart and secondPart are extracted from the original code.

In the child class you override it to read:
originalMethod() {
firstPart();
middlePart();
secondPart();
}

where middlepart implements that new functionality.

A second approach would be to implement originalMethod() as shown for the child class and simply stub out middlePart() in the superclass and have the child class override that.

- Scott



I like the original idea. I like the 2nd idea here much better if indeed firstpart and secondpart are the same for all implementations.
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic