• 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

Questions on the Following Design

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

The following code Snippet is used in our current project.



When I spoke to the architect I mentioned why don't we just allow the subclasses to override the MethodA() and MethodB() through normal inheritance. His response was that all common code should be in the base class and subclass should just implement their specific needs.

However, we can just call the super.MethodA() etc from the subclasses, but his argument was that developers may not when to call super.MethodA(), that is either in the beginning of the method or end.

The major concern that I had was that the BaseClass and SubClass will have a dependency. For example if a subclass does not require the specific behaviour of the baseclass it must put the smarts in to resolve this issue. Or vice versa the base class will have to put the smarts in.

Is there a pattern that will guarantee this setup and there won't be any of these dependences.

Thanks in advance,
Shameer
 
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can think of strategy pattern to be used for this purpose.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you showed uses a variation of the Template Method pattern - not a bad idea in general. I'm not sure I understand your doubts, though - could you please elaborate on what bad things you think could happen, perhaps with an example?
 
Shameer Subedar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example that I have is this:

The Base Class, say methodA(), which is called from another class which is part of a state management framework will call MethodA() on a state change and finally call methodB() on exiting the state.

When we subclass to create specific state handler, the OnInternalMethodA() will be called by BaseClass MethodA(), after MethodA() has finished doing executing its code.

My Concern is that the Specific state handler (subclass) may have no need for the behavior just executed in the base method.

This will then lead to code in the BaseClass methodA() doing something along the following lines of :

if( currentstate == somestate || currentstate == somestate2)
doThis();
else
dontDoThis();

I guess this could be overcome by allowing the subclass just override the MethodA() and call super.MethodA(), if it requires the behavoiur.

The other example could be that, the subclass does require the functionality of the baseclass MethodA(), but only after it has finished executing its code.

Again, the above issues will not be an issue if the assertion made by the architect is that BaseClass will handle all the common behaviors and we won't have any of the issues mentioned above.

From my reasoning, the design won't be flexible to handle the above cases if and when they do arise.

Thanks in advance and please advise.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I share your discomfort with this. The designer tries to enforce a design decision against all attackers. This might be appropriate for system integrity in critical security methods, but it makes the classes too inflexible for a general practice.

It's an interesting balancing act ... should a design provide a framework that enlightened developers can use productively, or should it rigidly enforce decisions made at a point in time in defense against untrustworthy developers.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shameer, your examples are good examples for when the design breaks down and needs to be replaced by something more flexible. It's not clear to me that you really need to do this up front - that depends on how likely those cases are to occur.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic