Use inner classes with State Pattern in Java?

K

kk_oop

Any opinions for or against using inner classes to implement the State
pattern in Java? In particular, this would mean making the abstract
state class and its derived specific state classes all inner classes of
the context class.

Thanks for any input!

Ken
 
C

chuck

I have an application that uses exactly this approach. It works well
and is elegent since inner classes have a reference to the outer class
instance which is good for making methood calls on the context in
response to state changes.
 
K

Ken

chuck said:
I have an application that uses exactly this approach. It works well
and is elegent since inner classes have a reference to the outer class
instance which is good for making methood calls on the context in
response to state changes.

This is what I have found as well. It surprises me, though, that a
google search on "Java" and "State Pattern" reveals many samples, but
none that do this (though I did see something like this approach in the
book Applied Java Patterns).

Here is a problem I came across with this approach. My concrete states
have no attributes--just their state specific behavior. As a result,
in the context class, I wanted to define a static final instance of
each concrete state. However, when I try to do this, I get an error.
Apparently, I can't have a static attribute that is of an inner class
type.

How do you handle instantiating the concrete states?

Thanks!

Ken
 
J

Jeff Schwab

Ken said:
This is what I have found as well. It surprises me, though, that a
google search on "Java" and "State Pattern" reveals many samples, but
none that do this (though I did see something like this approach in the
book Applied Java Patterns).

Here is a problem I came across with this approach. My concrete states
have no attributes--just their state specific behavior. As a result,
in the context class, I wanted to define a static final instance of
each concrete state. However, when I try to do this, I get an error.
Apparently, I can't have a static attribute that is of an inner class
type.

How do you handle instantiating the concrete states?

Could you make all the methods of each inner class final?
 
K

kk_oop

Jeff said:
Ken wrote:

Could you make all the methods of each inner class final?

My goal is to only have one instance of each concrete state class used
by all instances of the context class. That way I won't end up with a
lot of extra objects laying around waiting to be garbage collected. I
don't think that I get that by making the methods of the inner class
final--or even static final.
 
P

Patricia Shanahan

Ken said:
This is what I have found as well. It surprises me, though, that a
google search on "Java" and "State Pattern" reveals many samples, but
none that do this (though I did see something like this approach in the
book Applied Java Patterns).

Here is a problem I came across with this approach. My concrete states
have no attributes--just their state specific behavior. As a result,
in the context class, I wanted to define a static final instance of
each concrete state. However, when I try to do this, I get an error.
Apparently, I can't have a static attribute that is of an inner class
type.

How do you handle instantiating the concrete states?

Thanks!

Ken

Do your concrete state objects need access to a context class instance?

If yes, you need a separate instance of each concrete state for each
context class instance.

If not, declare each concrete state class to be "static", decoupling it
from the need for a surrounding outer class instance, and you will be
able to create static final fields that reference concrete state objects.

Patricia
 
R

Roedy Green

Any opinions for or against using inner classes to implement the State
pattern in Java? In particular, this would mean making the abstract
state class and its derived specific state classes all inner classes of
the context class.

Thanks for any input!

I just finished writing a set of finite state automata parsers that
worked that way, only I used enum constants for the states, with a
rich set of methods on each enum constant.

Annoyances: inner classes can't have statics.
There are odd rules about what you can access when.

You can't derive anything from an Enum.

I ended up using static for intercommunication. I think had I used
explicit inner classes I might have more easily avoided that.



--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top