• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which Design is Better and why ?  RSS feed

 
apurav chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this case:
A and Service are both classes.


Class A{
Service s;

void getSomething(){
s.printName();
}
}

NOw my requirement changed wherein implementation of printname() method will differ in some case. So i have changed the scenario with:

class A{
IService s;

void getSomething(){
s.printName();
}

Service1 implements IService;
Service2 implements IService;

So everythings works fine. But my question is what is the best architecture when in Service2 , not all method implementation are changed and rest methods remain same as Service1.

Should i make it like:
Service2 extends Service1 implements IService; (For methods whose implemenation remain same, i could call super classes implemenation)

-------------OR-------
Should i move the resuable code into some another class and use that in the two implemenations so that no parent-child relationship is there?
--------OR-------
Do i really need an interface over here if i am extending Service2 from Service1 for overriding a methods implemenation. And i am not sure, if in future, other implemenation of these may also come?






 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about:
This design allows you to be flexible in what class A accepts (anyone is free to implement Service as they see fit). AbstractService then defines default implementations of the methods, and the concrete classes Service1 and Service2 can override any methods they need to.
 
apurav chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Stephan, Thanks for replying.

Tell me one more thing :







HI, how can i implement something wherein i could get the extension from the class whose method is called? Question is weird , i know
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the point of view of the JVM, Service1 executed both of those methods. (Since Service1 doesn't have a definition for doSomething() it used the base class's).
What you're trying to do is the opposite of the intent of inheritance; I don't think it's possible (at least not reasonably so).
 
apurav chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what the best way to design such a thing? I know its not possible, but my intention is to know the best possible alternative?
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just code to the Service interface. Drop AbstractService since inheritance doesn't provide what you're trying to do here. You will either have to duplicate the code in Service1 and Service2 that was shared in AbstractService or create a separate class to handle those methods.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
apurav chauhan wrote:So what the best way to design such a thing? I know its not possible, but my intention is to know the best possible alternative?


So what you're saying that in this context you want service1.getExtension() to return "pdf". But in other contexts you want service1.getExtension() to return "txt"? What is distinctive about the different contexts, then? How would you describe which one is supposed to be returned?
 
apurav chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
context would be differentiated when i ll call a method that is exclusive to Service1 and not existing in AbstractService.

So what i am trying to do is:

if a method is called from the parent class, then getExtension should also be returned from the parent even though i have overriden it in child class.
And if a method is called from child class, then getExtension should be returned from the child.

I dont wanna put if else conditions in the calling class A. It should not be coupled to this behaviour. Some design in Service1 or AbstractService or any other thing that would do.

I know nothing is impossible if you have mastered the designs. So i am sure experienced guys would know this
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. Polymorphism prevents this. And you shouldn't want to either. If you have a situation where you need this, then you have a poor design and should rethink the structure of your program.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!