File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes A base type limited to two distinct subtypes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "A base type limited to two distinct subtypes" Watch "A base type limited to two distinct subtypes" New topic
Author

A base type limited to two distinct subtypes

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
For fun I'm making a generic implementation of the Boyer Moore Algorithm which can search any random access list for a given sublist.

I'm having trouble with the abstraction of a MatchResult. A MatchResult can either be a Match or a Failure, but I'd like a Failure to store additional information about the what index and element caused the failure.

Here's the solution I came up with but I am having doubts about its Javaishness (OK, so I made that word up).

Particularly, I think I may just be overlooking a more common solution or refactoring trick in favor of this bit of strangeness.
[ July 28, 2007: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What would worry you about someone providing a third implementation of MatchResult?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
What would worry you about someone providing a third implementation of MatchResult?

I guess worry is the wrong term. It just seems unnatural, like leaving Boolean open for extension.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Then my advice would be to simply get over it...

Seriously, why unnecessarily restrict your options in the future? Just use an interface and two top-level classes. That's what I'd do.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I'd also suggest you read up on the "Open-Closed Principle". You can never know when it might make sense in a future application to subclass something you have produced.

By attempting to limit the subclassing of your classes you have at the very least prevented yourself from using any of the handy tricks which depend on it, such as "self shunt" for testing, or the Decorator pattern. Even something as simple as deriving a subclass which implements a sensible toString() for use in diagnostics is not possible.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Thanks for the advice guys, I'll probably go with the interface and two top level classes as Ilja suggested. I think my judgment was clouded on this issue because I first implemented this algorithm in another language where its natural to provide two separate and disparate data-type constructors. But as usual the *functional way* is different from the *object-oriented way*. If I'd done this in Java first, I probably wouldn't have even considered another way. Anyway, thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A base type limited to two distinct subtypes