in reply to Implementing State pattern

You didn't include an accessor in dstatenorm to fetch the NAME.

package dstatenorm; ... sub name { my $self = shift @_; return $self->{NAME}; }

Then in your code, request the name of the state.

print $driver->alertness->name

PS: don't use prototypes especially when writing object oriented code.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^2: Implementing State pattern
by Justin_BSI (Acolyte) on May 02, 2006 at 23:39 UTC
    I'm not actually looking for the name of the state in the "alertness" routine, instead I am expecting a reply of the driver's alertness value. In the State pattern, the state makes modifications to (or performs functions on) the values of the "owning" class. For instance, if I made a state for the driver that was called "dstatetired" I could have the alertness routine return $curAlertness - 2;. I would set the current state through routines in the driver class.

    That being said, thank you for reminding me about the accessor and I will quit prototyping in my OO code (I'm used to Java, so pardon me.)