Exforsys

Online Training

Composite, State or Strategy? Not again!..

This is a discussion on Composite, State or Strategy? Not again!.. within the Software Patterns forums, part of the Testing category; Hi there, I have a class "Task" that *can* contain many subtasks. Various aspects of the Tasks behaviour ...


Go Back   Exforsys > Testing > Software Patterns

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-31-2004, 08:02 PM
Tobin Harris
Guest
 
Posts: n/a
Composite, State or Strategy? Not again!..

Hi there,

I have a class "Task" that *can* contain many subtasks. Various aspects of
the Tasks behaviour change depending upon whether it contains subtasks or
not.

Currently these varying behaviours are implemented using "if-else"
statements within each method. For example:

public TimeSpan GetDuration()
{
if ( this.subTasks.count > 0 )
{
return sumOfMySubtasksDurations();
}
else
{
return myDuration;
}
}

There is lots of this type of thing going on. I'm at the stage where the
class is 300+ lines of code long and I'm thinking of breaking it down. My
obvious choice is to create two State objects for the Task -
CompositeTaskState and LeafTaskState. The task will assign itself a new
state every time a subtask is added or removed, at runtime. This will create
something like the following:

public TimeSpan GetDuration()
{
return myState.GetDuration();
}

The state itself might be added in the following way:

public void AddSubtask( Task t )
{
subTasks.Add( t );
myState = new CompositeTaskState( this );
}

public void RemoveTask( int i )
{
this.subTasks.RemoveAt( i );
if ( subTasks.Count == 0 )
{
myState = new LeafTaskState();
}
}

You get the idea. I had considered a kind-of-strategy pattern, but this
would mean me creating many strategy patterns for only one single axis of
change - somewhat overkill. Similarly, I would have considered composite,
except this doesn't allow for runtime changes or behaviour? Or does it!?

Any thoughts apprecitated?

Tobes


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:00 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.