I have a hierarchy of objects that will all have similar behavior. I want to separate the behavior from the POCO definitions. Since the behaviors represent moving the objects to various states, this seems to me like a job for a State Pattern. But, it isn't as simple as there being a single definition for each function since each object may have slightly different behavior.
For example, let's say I have the following classes, based on an abstract base class:
public abstract BaseClass
{
public int Property1 { get; set; }
public int Property2 { get; set; }
}
public Class1 : BaseClass
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
}
public Class2 : BaseClass
{
public string PropertyC { get; set; }
public string PropertyD { get; set; }
}
The hierarchy represents the different types of objects. Let's also say that the objects all follow the same basic workflow: Submitted, Approved, Executed, Closed.
Now, the behavior of each function is also hierarchical, meaning that calling the function Approve() on Class1 should be the same as calling the inherited behavior from the BaseClass, but Class2 would override the Approve() function since that type follows a different approval process.
I'm lost trying to apply the State Pattern to these objects. I could choose to put the functions on the objects themselves and inherit them that way, and that works fine, but it breaks the POCO design. I could also implement the Approve() function with a switch statement for each object type, but that breaks my polymorphic design.
How can I apply a State Pattern to a multi-layer polymorphic Object definition and stay consistent with design principles.
Update: Let me clarify, I think that functions that do other things besides act on the object do not belong on the POCO. Ex: the Approve function is going to send emails and and trigger events in other systems, not just modify the state of the object. Maybe that's just me.
So I agree with everyone here that it wont break your POCO design. For example, it could looks something like this:
And a few different examples of running might look like this: