Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Decision Trees and the Strategy Design Pattern

by pdcawley (Hermit)
on Dec 26, 2002 at 06:54 UTC ( [id://222302]=note: print w/replies, xml ) Need Help??


in reply to Decision Trees and the Strategy Design Pattern

I reckon that what you're looking at here is the 'State' pattern rather than a 'Strategy'.
package Ticket; use strict; use warnings; ... sub state { my $self = shift; $self->{state} ||= TicketState::New->new(); } sub set_state { my $self = shift; my $new_state = shift; push @{$self->{statelog}}, $self->{state}; $self->{state} = $new_state; return $self; } sub send_to_supervisor { my $self = shift; $self->state->send_ticket_to_supervisor(@_); } sub approve { my $self = shift; $self->state->approve_ticket($self, @_); } sub reject { my $self = shift; $self->state->reject_ticket($self, @_); } package TicketState::New; ... sub send_ticket_to_supervisor { my $self = shift; my $ticket = shift; my $supervisor = $SUPERVISORS{$ticket->topic}; # Deliberately naiv +e $ticket->set_state(TicketState::PendingApproval->new); $supervisor->accept($ticket); return $ticket; } sub approve_ticket { die "You cannot approve a ticket in this state" } sub reject_ticket { die "You cannot reject a ticket in this state" } package TicketState::PendingApproval; sub send_ticket_to_supervisor { die "Ticket was already sent to a supervisor!"; } sub approve_ticket { my $self = shift; my($ticket, $supervisor) = @_; $ticket->set_state(TicketState::Approved->new ->set_owner($supervisor)); $supervisor->editorial_group->accept($ticket); return $ticket; } sub reject_ticket { my $self = shift; my($ticket, $supervisor) = @_; $ticket->set_state(TicketState::Rejected->new ->set_owner($supervisor)); $ticket->author->accept($ticket); return $ticket; } package TicketState::Rejected; # Almost indistinguishable from TicketState::New use base 'TicketState::New';
And so on.... Appropriate use of inheritance would mean that individual states would only have to know about the valid actions in that state, and their associated state transition rules, and it's easy to plug new states into the system. I've also found that when I use the State pattern it's easier to use meaningful names for actions without having to worry about whether an action is 'allowed' because the State system handles that for me -- I just move any state dependent behaviour into the appropriate state classes (or the approprate parent state object) and delegate happily.

One problem with the State pattern is that, until you're used to it, it can be hard to get a handle on the behaviour of the 'whole' system but I'd be tempted to argue that you should be reading your tests and use cases for that.

Replies are listed 'Best First'.
Re: Re: Decision Trees and the Strategy Design Pattern
by djantzen (Priest) on Dec 27, 2002 at 06:54 UTC

    State is definitely an important aspect of this, so I like the idea of maintaining that as a separate entity. I especially like the ability to store past states, as in some cases it may be necessary to step backward in the history of an entity in the system. I've been hoping to avoid hardcoding specific subroutines like send_ticket_to_supervisor, but on the other hand, the fact that those are in a distinct "logic" module means that different behavior could be achieved by overriding those methods or writing a new module.

    Thanks for your thoughts pdcawley :)

      The thing is, at some point you're going to have to code the 'what happens next' process. It may make sense though to add a do_next_thing method to your state class and then write:
      sub TicketState::New::next_action { 'send_ticket_to_supervisor' } sub TicketState::Pending::next_action { 'send_to_editorial_group' } sub TicketState::do_next_thing { my $self = shift; my $action = $self->next_action; $self->$action(@_); }
      By doing things this way you get to give meaningful names to the individual actions handled by a state, but still have a simple dispatch system that simply calls $ticket->do_next_action on every ticket it dispatches.

      If you're using the state simply to control the flow of an object through a system and you've not got other state dependent behaviour then you can probably get away with having a single state class and a handy dandy config file but I don't think I'd recommend starting out with one.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://222302]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-23 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found