aspose file tools*
The moose likes Beginning Java and the fly likes Interfaces: extend by pairs, or implement in pairs? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces: extend by pairs, or implement in pairs?" Watch "Interfaces: extend by pairs, or implement in pairs?" New topic
Author

Interfaces: extend by pairs, or implement in pairs?

Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 567
    
    4

I have three classes of object, most of which must implement two out of three interfaces. The interfaces look like this:


Most of my classes must implement two of the three above, such that my class definitions (before implementing the necessary methods) would look like this:



That works fine, but I am wondering if pairing the interfaces into subinterfaces is a defensible methodology. For example, all classes that act like Producer must implement both the Source and Sender interfaces. And all classes that act like Relayer must implement the Sender and BlackHole interfaces. I could define two subinterfaces like this:



I could then define my classes like this:



Within the class definitions, it makes no difference, as I will have to implement the same methods either way. But it seems more self-documentary to create the subinterfaces from their parent interfaces and name them in ways that reflect what the classes that implement them must actually do.

Is this a good idea, obfuscatory, or something else?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Both methods (not “methodology”) look correct. The question you should ask is:-
Is there such an entity as a factory in my “world”?
If so, write a Factory interface.

By the way: miss out the methods' access modifiers in the interfaces; the methods are public by default in a public interface.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
You only seem to have considered the definition of your interfaces so far. What about their use ? If you are going to need variables that act as both BlackHole and Sender at the same time, then you are going to need the Relayer interface or you will end up with code with a lot of casting in it.


Joanne
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8223
    
  23

Stevens Miller wrote:That works fine, but I am wondering if pairing the interfaces into subinterfaces is a defensible methodology.

Seems fine to me. What I'm not so sure about is whether Producer, Relayer and Consumer are actually classes; they look much more like interfaces to me as well.

I think, if I was doing this, I'd then be tempted to create skeleton implementations of each of them with abstract classes (AbstractProducer, AbstractRelayer and AbstractConsumer?) that cover the majority of cases you're likely to need. That would then save you the trouble of coming up with "interface names" for interfaces that basically define the same types as your classes currently do.

However, it's only one possibility; there are plenty of others.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 567
    
    4

Thanks, folks! Just what I needed in the way of feedback.

I work alone, so there's no one in the next cube to talk to about things like this (because, well, there's no "next cube" here).

Much appreciated.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8223
    
  23

Stevens Miller wrote:Thanks, folks! Just what I needed in the way of feedback.
I work alone, so there's no one in the next cube to talk to about things like this (because, well, there's no "next cube" here).

Glad we could help. Welcome to JavaRanch, and feel free to think of us as your "next cube".

Winston
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 567
    
    4

"Welcome to JavaRanch?"

Good gracious, Winston. I know I've been away for a while, but don't you remember me?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8223
    
  23

Stevens Miller wrote:Good gracious, Winston. I know I've been away for a while, but don't you remember me?

Ooof. My apologies. For some reason I read '2' under your 'Number of posts' (I think I must have read your 'Number of cows'), and didn't even note the name.

Put it down to Alzheimer's...or too much beer. That's my excuse.

Winston
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 567
    
    4

If the reason is too much beer, then I forgive you.

(But only if you're sharing.)
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 567
    
    4

I was going to mark this thread "resolved," but that button isn't showing. Does it go away when a staffer marks it as "thinks it is resolved"?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces: extend by pairs, or implement in pairs?