Perhaps strategy pattern isn't what I'm after. Say my code looks like this (pseudo version):
class Machine
{
private Stack<State> _internals;
public void DoOperation(Thingy x)
{
switch (x.operation)
{
case Op.Foo:
DoFoo();
break;
case Op.Bar:
DoBar();
break;
case Op.Baz:
DoBaz();
}
}
private void DoFoo()
{
// pushing and popping things from _internals, doing things to those States
}
private void DoBar()
{
// similarly large method to foo, but doing something much different to _internals
}
private void DoBaz()
{
// you get the idea...
}
}
Foo, Bar, and Baz are rather complex methods (not extremely long, just deserve separating) so I want to break them into classes with a common interface, a la strategy pattern. The problem is, I can't encapsulate _internals
in those classes. I mean, I could pass it into the Execute
method on those classes, but that seems like a bad way to go. The internals persist longer than the single operation, so the strategy classes can't "own" the internals themselves. Multiple different operations could be done on this Machine, with different Thingy's passed in.
Is there a different route you can suggest?
edit
This is kind of a state machine, but not in the sense that one operation is only valid in a particular state. _internals
is a stack of states instead of just the current state. Any of the three operations can be done at any time.