2

Lets say i have two classes A and B, which have theirs internal state represented by enum State. Class B extends A to add new functionality, but it also adds new internal state. To be precise, it adds fine-grained state support, by dividing one of the A`s state into two new states.

I also need to use PropertyChangeSupport to monitor state changes.

Currently I have all states defined in class A in enum State. I have protected methods setState() and getState(). To check on state from the outside, I wrote methods isAvaible(), isComplete(), etc. These methods are present only in appropriate class, depending on their hierarchy and state support.

I realy dont like the idea, having all states defined in parent class. How do I implement this correctly in Java? As far as I know, it`s not possible to use inheritance with enums.

1
  • 3
    You can't extend an enum - it would be easier to understand if you could show the structure of your code as code instead of describing it. Commented Apr 8, 2012 at 19:59

2 Answers 2

4

Delegation is preferred to inheritance.

enum StateA
{
       AVAILABLE , COMPLETE ;
}

class A
{
      private StateA state ;
}

enum StateB
{
     AVAILABLE_TO_PUBLIC ( StateA . AVAILABLE ) , AVAILABLE_INTERNALLY ( StateA . AVAILABLE ) , COMPLETE ( COMPLETE ) ;

     StateB ( StateA sa )
     {
          this . sa = sa ;
     }

     private StateA sa ;
}

class B
{
      private StateB state ;
}
Sign up to request clarification or add additional context in comments.

6 Comments

isn't it so, that AVAILABLE_TO_PUBLIC and AVAILABLE_INTERNALLY would be equal in this modeling?
@loybert, AVAILABLE_TO_PUBLIC and AVAILABLE_INTERNALLY would not be equal in this model. AVAILABLE_TO_PUBLIC . equals ( AVAILABLE_INTERNALLY ) should be false. This "adds fine-grained state support, by dividing one of the A`s state into two new states."
ah cool. thought this would have the same internal integer-value, because of the same mapping to StateA.AVAILABLE
Why don't you just combine StateA and StateB into one enum?
@JamesGoodwin Actually, I like your idea better than my answer. I interpreted Puty's "dont like the idea, having all states defined in parent class" as meaning he did not want something like that. But if it were me, I would probably do as you recommend.
|
1

I think you want to ditch the Enum concept except as something private to class A to help you program it. A's external state is available through methods like isAvailable() and isComplete(). Class B would extend A but should know nothing of A's enums, though it may have a private one of its own. If a change needs to affect A's state, and hence A's enum StateA, do it through method calls.

The "state" of an object of either class A or B should be determined, accessed, used, or whatever through its methods, not by accessing an enum value. In other words, an instance of A or B in itself is a complete definintion of state. It may not name the state, as an enum would, but it tells you everything you need to know about what the "state" allows and can do and how it can be changed.

Alternatively, you could set up an enum visible to everybody and define it globally. Then enum StateA is completely independent of class A, solving the basic design problem. Classes A and B, and others, would then depend on StateA to provide the language they use to communicate with each other and the rest of the system, making for a better design.

I kind of like the first idea better, because, by tweaking A and B, and extending them, you can really fine tune the meaning of each state and even add new states without making a formal announcement. (Adding a value to an independent StateA is not going to be a big deal, but programmers using StateA might wonder if they should be using the new value--a minor nuisance.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.