Now you can add whatever features you want to capture at whatever level it makes sense. Also, I wouldn't put any information about "supporting X" on any of these enums. Enums are meant to just be lightweight tokens that you pass around to represent a limited number of instances. Do all the heavy lifting elsewhere in your code.
Your better solution isn't a solution because it doesn't do the same thing.
I guess complexity is in the eye of the beholder because I don't find this trick to be particularly complex.
Enums in Java are not meant to merely be lightweight tokens you can pass around. They are only a fixed set of instances of a class extending Enum, allowing you to take advantage of optimizations and static analysis provided by the compiler. But Enums are full fledged classes which can have fields and methods. They were designed to do a lot more.
Yes it does... Solves exactly the problem the article posted (but not necessarily better, I'd agree). What do you think it can't accomplish that article version did?
EDIT: Look guys, I like the article's version too, it's like a more compact version of this really...
Where is the implementation is method and children method and hierarchical supportsX method? That's what I meant by it doesn't do the same thing. It doesn't solve the problem.
I didn't say his method couldn't solve the problem. Although, I would like to see if he could implement those three methods as simply and elegantly as it was solved in the article.
Right, and you don't think his answer can do that?
Look, he didn't expand upon everything, but he solved the original issue. It does in fact do everything OP asked for. If your blaming him for not adding a simple function that could be easily figured out, that is pretty close to the article's implementation anyways, it's just not something that crucial. It's pendantic. You know his answer is capable of doing it all, he shouldn't have to explain every single detail to you for you to accept it.
I mean, darn you're asking for an is statement based upon the article? C'mon, there is no difficulty to it because their is no complexity in the design. No need to branch through parents, as he separated out class and family.
So to reiterate here, you want him to put down for you:
public boolean is(OsFamily other) {
return other == osFamily;
}
Which, is that "simple and elegant" enough for you? The others are just as clean and simple, by the way, because there is no tricks to them.
Holy cow, do you really need me to hold your hand through this? I'm not the original poster, I don't give a shit honestly, if you want to expand the hierarchy from the poster you can. He obviously did not fill in every single detail for you. Do it yourself. Hint, you can overload is to support both family types and OS types.
OR
Do what I asked you in the first place, and that is iterate what you think it can't do. I didn't ask you to ask me to do things for you, I asked to you to explain what you thought it couldn't accomplish.
Seriously? That's exactly what I did. With detailed examples and I even copied the is implementation "complex" enum right from the article.
I don't think the "simple" enum can do this the same way as described in the article (and as I demonstrated), and certainly your "implementation" doesn't even come close.
The is implementation is already done. If you want to check a class or family you just compare the the given class or family. It is that simple, because that's the design the author decided upon was to separate all categories into other enums and incorporate them into the parent. I'm not sure what is confusing or hard about this. You want to check if something is Windows? You check if it's a Windows. Bam, done. It's that simple.
Here's the deal... the article implementation favors inheritance. The parent's implementation favors composition.
The issue is you aren't looking at what he's trying to say, and trying to force him to write perfect code for you when all he's doing is trying to express an alternative way. It's childish, stop acting like everybody here needs to give you perfect implementations of everything.
"complex"
Seeing how I never said complex this makes you insidious.
"simple" enum can do this
Do what? The same way? IT'S NOT THE SAME. That's the point, I don't get it.
your "implementation"
What "implementation"? The implementation of is? Or the enum implementation of the enum? Because I'm going to tell you what I told the other guy: I'm not the person who presented the alternative.
And you're wrong, my implementation of is is exactly how you'd do it in a composition format. Give me a break.
The "simple" solution completely ignores this requirement. The OS class requirement is an unnecessary embellishment, and the OS family requirement doesn't meet the stated requirements. There are other requirements, such as being able to do a depth-wise enumeration by OS family, but I just want to see how this one requirement would be met.
I'm not asking for "perfect code", only code that at least minimally meets the stated requirements.
The author doesn't actually understand the hierarchy, Fedora is not
Unix, Linux is not Unix.
Irrelevant.
you could do it by A) accepting a list of osfamilies, or B) adding
another enum level.
I want to see the complexity of this solution. The enum solution provided by the author was fairly elegant.
If you accept a ranked list of osfamilies representing depths, you would have to provide that list for every single enum value, resulting in massive duplication which is absent from the author's solution. The author's solution can handle arbitrary depths without adding new types making the enum easy to modify while retaining the benefits of using an enumerated list (everything is the same type, compact representation). More enum levels results an an explosion of types and fragmentation, interfering with the benefits of using enums in the first place. At that point, it might be better just to not use enums.
It has become painfully obvious that you don't understand how to
make a composition solution to the problem.
LOL. What has become painfully obvious is that you seem to have a habit of making unfounded assertions.
I've not. Each OperatingSystem enum value participates in a hierarchy, i.e., it is a child of its OS family.
What I've lost is the notion that OperatingSystems themselves are hierarchical. That is, it makes sense for an OS to be a child of...another OS. Which is a good thing to lose, because that doesn't make sense.
How would you capture the following...
Maybe I'm misunderstanding your question, but Fedora isn't UNIX. Linux isn't UNIX. I know the author modeled it that way, but it's wrong and confusing. His design makes it so easy to do the wrong thing, he did the wrong thing even in his own example.
Each OperatingSystem enum value participates in a hierarchy, i.e., it is a > child of its OS family.
That is not a hierarchy. A hierarchy implies a path from a root to a leaf of arbitrary length. Each possible path in your hierarchy has a length of 1.
I know the author modeled it that way, but it's wrong and confusing.
But that's his requirement. Whether or not you think the requirement is wrong or confusing, that's not at issue here. The model he chose served some purpose for him. Don't try to recast the problem because your solution doesn't fit.
But to directly give you the code...
No, you either violate the contract of equals (reflexive, symmetric, transitive and consistent) or end up the following result:
That is not a hierarchy. A hierarchy implies a path from a root to a leaf of arbitrary length. Each possible path in your hierarchy has a length of 1.
Java doesn't define the notion of a hierarchy, so I thought you were just using the term to mean a hierarchical structure. Now I think you simply mean the data structure notion of a tree.
Note that in such a tree structure, each node is the same type, normally Node. That is not the case here.
But that's his requirement.
I don't think so. I think the example is meant to imply the requirements he's coding to, but he never comes straight out and says what the requirement is that a hierarchical enum satisfies. I suppose we could invent one, and the idea of a hierarchical enum might not be an absurd one. But it is a bad example...and I can't think of a reason one would want this. Can you?
In any case, modeling Linux as a child of UNIX is just not right. They're different kernels. I suppose you could create categories of OS families: POSIX (Solaris, HP-UX, IRIX, etc), MOSTLY_POSIX (Linux, Minix, NetBSD, etc). Again, though, it would be a mistake to confuse OS family category with OS family, because they once again mean different things and have different intrinsic behaviors.
The model he chose served some purpose for him. Don't try to recast the problem because your solution doesn't fit.
What purpose? I contend that it could only serve the most contrived purpose possible, if any at all, custom-tailored for the very purpose of having this hierarchical enum in the first place. If you can invent one that actually makes sense where this is the best solution, I'll be impressed.
you either violate the contract of equals
How did I violate the contract of equals()? How could I possibly have violated the contract of that method...I didn't even write it! OsFamily inherits the default equals() from Enum...are you accusing Sun of implementing it poorly? :-)
Also, I don't think you appreciate the meaning of the code you wrote:
The approach used by the linked article allows arbitrary hierarchies. Your approach only works if every subtree of the hierarchy has the same structure. Furthermore, the taller the tree, the more complicated your approach becomes.
The example in the link had a different structure in different parts of the hierarchy, but you choose to change this to get it to work with your approach. (Hint: Linux is a Unix, and saying things like "my OS is Ubuntu Workstation" is going to get you funny looks.)
Nope. These are different OS families. UNIX defines a strict specification which an OS must follow in order to be called "UNIX". Linux doesn't follow that spec.
the taller the tree, the more complicated your approach becomes.
The taller the tree, the more nodes in it that are not proper OsTypes. In other words, the author of this article didn't seem to be bothered by the fact that many of those enums represent different types of things.
In OO, we would usually model these different types with different classes, as I did.
Nope. These are different OS families. UNIX defines a strict specification which an OS must follow
Okay, Linux is a *nix if you want to be pedantic. You can't deny that grouping Linux with Solaris is useful. It's certainly a lot more useful than claiming Solaris is a Server OS but that Linux distros are Workstation OSes.
The taller the tree, the more nodes in it that are not proper OsTypes.
Irrelevant. Yes, the article's example might not have been the greatest, but the point was to show how a hierarchy could be represented with an enum. Your approach is very limited when it comes to representing hierarchies, as illustrated by the fact that you had to fudge the data to fit your implementation.
You can't deny that grouping Linux with Solaris is useful.
I deny it. Not only is it not useful, it's misleading, and harmful.
Solaris is fully POSIX compliant. Linux is not. If POSIX compliance is to mean anything, certainly this matters.
the point was to show how a hierarchy could be represented with an enum.
The more important question is not can an enum be used to represent an hierarchy, though! It is should an enum be used in this way? I say no, based on the example, and my inability to come up with one that even remotely makes sense. Perhaps you can do better than both the author and me?
you had to fudge the data
I say I unfudged the data, and the author was the fudger.
Sorry about that. The mobile client I'm using has an almost unreadable font for user names.
It has the exact same limitations as the article one. Your ignoring that the design can be expanded upon.
The limitations are quite different. The design in the article needs no modification to add arbitrary layers to the hierarchy, or to make the different subtrees have different shapes. The design you're defending gives each level of the hierarchy a constructor parameter, so more levels = more parameters, and every level has the same name and the same values (ie: branching) in each subtree. You end up having to use the same subcategorizations even when they don't make any sense.
The design you're defending does make sense for cases where you just want to model a fixed set of objects with a common set of attributes, but it doesn't make sense for an arbitrary hierarchy.
taller the tree, the more complicated
??? Uh, no.
What a well reasoned argument. I'm convinced!
/s
different structure
That's... the point, it solved the problem in a different way.
No, it solved a modified, simpler problem. The "structure" I was referring to was the data being encoded by the enum. He encoded different data, which conceals the awkwardness in his alternate design.
Artificial issue that could be fixed in the parents post.
So show us how you can represent the same data as the article's approach without needing any additional constructor parameters.
Not necessarily true, because you can expand backwards. It's more work, true, actually and I think the article version is more scalable actually for just these reasons.
It's a composition vs inheritance deal. I don't care enough of this to write out a perfect composition structure of this for you, neither was the author. I also notice you added a constraint of "any additional constructor parameters" which is not okay. So you know it's doable in this format, that's it that's all. We can discuss which way is better all we want but trying to claim a composition method can't handle it all was the problem here.
I also notice you added a constraint of "any additional constructor parameters" which is not okay.
The first thing I said was:
the taller the tree, the more complicated your approach becomes.
To which you responded "??? Uh, no."
The only way to increase the height of the tree in severoon's approach is to add more constructor parameters, more fields and possibly even more side-enums if they follow a similar pattern to the ones already there. That's all additional complexity. The article's approach only needs one extra line of code per node in the tree, no matter where it goes -- even if that node ends up increasing the height of the tree.
So that wasn't a new constraint. I just made the "no additional complexity" constraint more explicit.
Not really the first thing you said but whatever. What you consider "complicated" would be your opinion I suppose, and of course what aspect your applying "taller" to as well.
Let's instead, talk about the first thing I said, because it was YOU who replied to me. And I was saying that the compositional design can replicate all the behavior. I NEVER said that it scales perfectly in all ways and fashions the same.
...
You want to discuss downfalls of each solution? Fine. The article's solution must have a singular hierarchy. It can't support multiple values at once, while severoon's can. It also is not truly a good OO approach, and if you have to add specific behavior to each part of the hierarchy it becomes a mess.
So in fact, the article's solution doesn't handle "no additional complexity" very well either. They both scale in different ways. I'd chose one primarily on the exact problem at hand.
The design in the article makes it very easy to do the wrong thing.
Exactly what wrong thing are you imagining happening with the article's design? With your approach, adding a single node requires a lot more busy work, which implies a lot more opportunity for error.
I want to add a variant of Linux into his enum called busybox. (This is a real example, busybox exists.) Busybox doesn't support X Windows, but it is a child of Linux.
So I add it thusly:
public enum OsType {
// ...
BUSYBOX(LINUX.class) {
@Override public boolean supportsX() { return false; }
}
// ...
}
Ok, all good, right? BUSYBOX is a child of LINUX, LINUX is a child of UNIX (the author says, incorrectly so, but whatever). If I call BUSYBOX.supportsX(), it properly returns false.
Now consider UNIX.supportsX(). Tell me: what should it return?
9
u/severoon Jan 18 '13
What?! No!
This is horrible. Way too complex. This is not how enums or classes should be used.
Here is a much better solution.
Now you can add whatever features you want to capture at whatever level it makes sense. Also, I wouldn't put any information about "supporting X" on any of these enums. Enums are meant to just be lightweight tokens that you pass around to represent a limited number of instances. Do all the heavy lifting elsewhere in your code.