• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do I Need To Use An Interface  RSS feed

 
mike housing
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm a little new to java and for whatever reason I can't figure out my problem for the life of me. Attached is the class diagram that I will attempt to explain.

The parent class is Search and it holds a public method called Search() which calls a private method that is in the parent class Search called ValidateSearch().

The parent class has two classes that extend it called NameAndAlias and Advanced.

The NameAndAlias class has two classes that extend it called Employee and Non Employee.

Right now Employee and NonEmployee override the ValidateSearch() method in the parent class Search, each doing something different.

The problem comes in where the other parent class Advanced has a class that extends it called External and that class overrides the ValidateSearch() method in the parent class Search, but it has the same exact code as does the overridden ValidateSearch() method in the Employee class. For whatever reason I can't figure out how to not have duplicate code in the overridden class of Employee and External and I think I'm having a hard time because of the two different branches.

Do I use an Interface or is there some kind of design pattern I should use. I hope this makes sense, and if it doesn't please feel free to ask me to clarify. Thanks.

Edited to break up the wall of text.
classDiagram.jpg
[Thumbnail for classDiagram.jpg]
Class Diagram
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface won't help directly, because interfaces don't contain an implementation. You could use interfaces along with composition to simulate a mixin, depending on what the functionality is and where/how it's used, though...
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you manage to have NameAndAlias extend Search? How can an Employee extend NameAndAlias?
Remember: if you say Foo extends Bar, you are saying that a Foo is a Bar. Do you really think an Employee is a Search?
 
mike housing
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know these names aren't the best names for these class and there not the actual ones, I just made these up real quick for this example. David I'm not really sure if I understand your answer.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A "mixin" (or "mix-in") is a class that exists only to be combined with other classes, providing some useful bit of functionality. Basically what David is saying is to put that duplicated alidateSearch() code in some other class (ValidateSearchMixin, perhaps), and then have the validateSearch() method of Employee and External call it. You can't truly "mix it in" using multiple inheritance, the way you can in some other languages, but you can use composition and/or delegation: an Employee might hold a ValidateSearchMixin instance in a member variable, and then delegate:



Instead of having a member variable, ValidateSearchMixin might have a singleton instance, and Employee.validateSearch() might simply call that singleton's method.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

Here is what David meant:

Interfaces are pure abstract classes, meaning they don't contain any definitions/implementations. It mostly contains Constants and method definitions. In your case an interface may not be helpful.
When you need to override a method, you need to extend that class. When you need to just use the method, use a has-a relationship which means create an object of that class and invoke methods on it.

Regards,
John Eipe
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!