The Artima Developer Community
Sponsored Link

Weblogs Forum
Java Enums want to be Classes

25 replies on 2 pages. Most recent reply: Dec 15, 2010 1:59 PM by Andrea Francia

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 25 replies on 2 pages [ « | 1 2 ]
Bruce Eckel

Posts: 875
Nickname: beckel
Registered: Jun, 2003

Re: Java Enums want to be Classes Posted: Oct 26, 2005 3:31 PM
Reply to this message Reply
Advertisement
> Yes, but, again, it isn't about modeling reality. In my
> opinion, the industry was poorly served by some early
> voices who touted the idea that OO involved modeling
> reality and that it was akin to data modeling and AI.
> It's taking us a long time to get over that.

I think the industry is poorly served by taking a purist view of anything. OO does, in fact, allow us to do a better job of modeling reality, and that was a large part of its intent. However, the map is not the territory and so there will always be some abstraction when (A) developing the model and (B) implementing the model. Saying that a model must be a perfect mapping is also a disservice, especially when it impacts the flexibility of the resulting solution.

I would also point out that design patterns generally have to do with implementation rather than mapping the world. So that community, at least, does not have any illusions about what is being accomplished.

What I find is that a system has two facets. First, the "modeling reality" part, which tends to drive the initial creation of the design, and which ultimately determines the usefulness of the result. Second, the pragmatics of putting the system together, which inevitably includes aspects that are not strongly related to the real world, but instead includes aspects to fit things together and to facilitate easy change (the latter is not usually based on the real world model, but rather on the realities of the customer situation).

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: Java Enums want to be Classes Posted: Oct 27, 2005 12:15 AM
Reply to this message Reply
> > Yes, but, again, it isn't about modeling reality. In
> my
> > opinion, the industry was poorly served by some early
> > voices who touted the idea that OO involved modeling
> > reality and that it was akin to data modeling and AI.
> > It's taking us a long time to get over that.
>
> I think the industry is poorly served by taking a purist
> view of anything. OO does, in fact, allow us to do
> a better job of modeling reality, and that was a large
> part of its intent. However, the map is not the territory
> and so there will always be some abstraction when (A)
> developing the model and (B) implementing the model.
> Saying that a model must be a perfect mapping is also a
> disservice, especially when it impacts the flexibility of
> the resulting solution.

I'm not a purist about anything, but I'll point out that modelling reality isn't the job. Our job is making maintainable software. The theory is that modelling reality makes the software better somehow, and I don't think that's true.

> What I find is that a system has two facets. First, the
> "modeling reality" part, which tends to drive the initial
> creation of the design, and which ultimately determines
> the usefulness of the result. Second, the pragmatics of
> putting the system together, which inevitably includes
> aspects that are not strongly related to the real world,
> but instead includes aspects to fit things together and to
> facilitate easy change (the latter is not usually based on
> the real world model, but rather on the realities of the
> customer situation).

That's just one way of doing it, however. And it's sad that many people see it as the only way because they start with this "modeling reality" focus that leads to a bunch of traps that they have to sidestep pragmatically. You can design with a behavioral focus from the beginning, and end up with very good designs that may only marginally graze the domain concepts and often that is a very decent thing to do.

A few examples. I once worked on an online auction system where the Bid class was the only one that mentioned the domain. If we used a "modeling reality" approach would have ended up with a substantially different design. The C3 project at Chrysler that spawned XP used Lines, Bins, Stations, and Parts as a classes to do employee payroll, again pretty much orthogonal to a "modeling reality" approach.

In general, I see value in using domain names when possible, but often when I look at the classes I've written with people they are not anything like what you would get from a standard domain model, but that's fine. They make sense in every other engineering aspect. No, I think that there are better alternatives to representational modeling and the focus we have on it is a mistake.

Bruce Eckel

Posts: 875
Nickname: beckel
Registered: Jun, 2003

Re: Java Enums want to be Classes Posted: Nov 2, 2005 3:57 AM
Reply to this message Reply
I must not be understanding something about what you mean here. It seems to me that OO is intended to make it easier to model the problem space. For example:

- The first OO language was called "Simula," and it was designed to make it easier to create simulations, which are models of reality. C++ drew fairly heavily on Simula's design.

- Domain Driven Design focuses on creating a domain model and building the system to concur with that domain model.

Perhaps you can point to other papers or opinions that explain your position.

amit dalvi

Posts: 1
Nickname: amitdalvi
Registered: Nov, 2005

Re: Java Enums want to be Classes Posted: Nov 18, 2005 6:02 AM
Reply to this message Reply
what you are trying to achieve could be done using interface. The Enum could implement a base interface

thomas last

Posts: 1
Nickname: tfh2
Registered: Mar, 2006

Re: Java Enums want to be Classes Posted: Mar 19, 2006 12:34 PM
Reply to this message Reply
delay interpretation of the opposite values until
later/needed/postinit by storing them as Strings...
    public enum Direction
    {
        NORTH("SOUTH"),
        SOUTH("NORTH"),
        EAST("WEST"),
        WEST("EAST");
 
        String opposite;
        Direction(String opposite) {
            this.opposite = opposite;
        }
        public Direction getOpposite() {
            return valueOf(opposite);
        }
    }
 
    public static void main(String...argv) {
        for(Direction step : EnumSet.allOf(Direction.class))
            System.out.println(
                "opposite of " + step + " is " + step.getOpposite());
    }

Martin Rusnak

Posts: 1
Nickname: martinr
Registered: Mar, 2007

Re: Java Enums want to be Classes Posted: Mar 27, 2007 4:47 AM
Reply to this message Reply
The problem can be solved by initializing the opposite field of each instance in the static statement:

public enum Direction {
NORTH,
SOUTH,
EAST,
WEST;

static {
NORTH.opposite = SOUTH;
SOUTH.opposite = NORTH;
EAST.opposite = WEST;
WEST.opposite = EAST;
}

Direction opposite;

public Direction opposite() {
return opposite;
}
}

This solution is easy and straightforward, so I don't see the reason why not to use Java Enums.

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: Java Enums want to be Classes Posted: Mar 28, 2007 8:11 PM
Reply to this message Reply
Yes, I didn't thing of that. I try to avoid static initialization blocks. It does seem kludgey, though, doesn't it? It's a shame that Java doesn't attempt to resolve forward initialization on statics within a class.

frankie fuentes

Posts: 1
Nickname: frankie609
Registered: Jul, 2007

Re: Java Enums want to be Classes Posted: Jul 18, 2007 2:04 AM
Reply to this message Reply
Mmm.. Interesting, BUT you might have a problem like this one -> http://www.javaworld.com/javaworld/javatips/jw-javatip122.html if you're going to use classes. Though I'm not aware if enum on java 5 solves the problem. Look at the link, got some interesting stuff especially if using classes as enum on j2ee containers.

Mark B

Posts: 1
Nickname: maboi
Registered: Sep, 2007

Re: Java Enums want to be Classes Posted: Sep 21, 2007 9:01 AM
Reply to this message Reply
> public enum Direction
> {
> NORTH(SOUTH),
> SOUTH(NORTH),
> EAST(WEST),
> WEST(EAST);
>
> Direction opposite;
>
> Direction(Direction opposite) {
> this.opposite = opposite;
> }
>
> public Direction getOpposite() {
> return opposite;
> }
> }
>
> Unfortunately, this doesn't compile. The compiler
> tells us that the field SOUTH can't be referenced
> before it is declared.

All you have to do is qualify the reference to the enumeration like this:

public enum Direction
{
NORTH(Direction.SOUTH),
SOUTH(Direction.NORTH),
EAST(Direction.WEST),
WEST(Direction.EAST);

Direction opposite;

Direction(Direction opposite)
{
this.opposite = opposite;
}

public Direction getOpposite()
{
return opposite;
}
}

Rob Woodward

Posts: 1
Nickname: robw
Registered: Nov, 2009

Re: Java Enums want to be Classes Posted: Nov 25, 2009 11:58 PM
Reply to this message Reply
>All you have to do is qualify the reference to the enumeration like this:
>
>public enum Direction
>{
>NORTH(Direction.SOUTH),
>SOUTH(Direction.NORTH),
>EAS T(Direction.WEST),
>WEST(Direction.EAST);

Sadly this doesn't work - I just tried it. Although it compiles, the forward references evaluate to null.

I went with the static block approach in the end.

Andrea Francia

Posts: 1
Nickname: francia
Registered: Dec, 2010

Re: Java Enums want to be Classes Posted: Dec 15, 2010 1:59 PM
Reply to this message Reply
Dear Michael,
thanks for your book.

I think you can make your second getOpposite() attempt lightly more compact.


enum Direction {
NORTH, EAST, SOUTH, WEST;

public Direction getOpposite() {
switch(this) {
case NORTH: return SOUTH;
case EAST: return WEST;
case WEST: return EAST;
case SOUTH: return NORTH;
default: throw new Error("Someone added a direction and forgot to"
+ "update this method ... ");
}
}
}

public class DirectionsExample {
@Test public void testOpposite() {
assertEquals(SOUTH, NORTH.getOpposite());
assertEquals(NORTH, SOUTH.getOpposite());
assertEquals(EAST, WEST.getOpposite());
assertEquals(WEST, EAST.getOpposite());
}
}

Flat View: This topic has 25 replies on 2 pages [ « | 1  2 ]
Topic: Conference: JavaPosse Roundup, Feb 22-25 Previous Topic   Next Topic Topic: The Holistic Approach to Software Engineering as a Way to Handle Complexity

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use