Is there a design pattern that expresses objects (an their operations) in various states?

Posted by darren on Stack Overflow See other posts from Stack Overflow or by darren
Published on 2010-05-17T00:37:14Z Indexed on 2010/05/17 0:40 UTC
Read the original article Hit count: 196

Filed under:

Hi

I have a design question about the evolution of an object (and its state) after some sequence of methods complete. I'm having trouble articulating what I mean so I may need to clean up the question based on feedback.

Consider an object called Classifier. It has the following methods:

void initialise()
void populateTrainingSet(TrainingSet t)
void pupulateTestingSet(TestingSet t)
void train()
void test()
Result predict(Instance i)

My problem is that these methods need to be called in a certain order. Futher, some methods are invalid until a previous method is called, and some methods are invalid after a method has been called. For example, it would be invalid to call predict() before test() was called, and it would be invalid to call train() after test() was called.

My approach so far has been to maintain a private enum that represents the current stateof the object:

private static enum STATE{ NEW, TRAINED, TESTED, READY};

But this seems a bit cloogy. Is there a design pattern for such a problem type? Maybe something related to the template method.

© Stack Overflow or respective owner

Related posts about design-patterns