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

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

Consider an object called a 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 specific order. Further, some methods are not valid until the previous method is called, and some methods are not valid after the method is called. For example, it would be unacceptable to call the prediction function () before calling test (), and it would be invalid to call the train () after calling the function ().

So far, my approach has been to maintain a private enumeration that represents the current state of the object:

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

But that seems a little vague. Is there a design pattern for this type of problem? Perhaps something is related to the template method.

+4
source share
3 answers

Well, for this particular case, I think you are thinking too much here. For example, should a distinction be made between a set of training materials and a set of test data in terms of their type? My suggestion would be to go with a factory template; you must have a MachineLearningAlgorithm factory with a "train" function that returns a Hypothesis object on which you can run a "test" or "predict". The train function should take the training data set as its parameter, and the test function should take the test data set as the parameter. Both datasets are likely to be of the same type because their shape / structure is identical, although the data contained in them is different. As for populating a dataset, this really should not be a problem with your machine learning algorithm; anyone using the algorithm should be responsible for providing these data sets. However, if you want to have some sample data sets, I would suggest plants for different pairs of data pairs for trains / tests.

  public interface Result { public double getDecisionValue(); public String getPredictedLabel(); } public interface TestResult extends Result { public String getActualLabel(); } public interface TestResults extends Iterable<TestResult> { public int getErrorCount(); public double getErrorRate(); } public interface Hypothesis { public TestResults test(Iterable<DataPoint> dataset, Iterable<String> labels); public Result predict(DataPoint datapoint); } public interface MachineLearningAlgorithm { public Hypothesis train(Iterable<DataPoint> trainset, Iterable<String> trainlabels); } 
+2
source

I think a state template can help you. For each state, you create a class that implements these methods in your path, and methods that you do not need, you can throw exceptions or do nothing. And your main class may contain a state object that will change depending on the state. Is it suitable for you? state design template

+2
source

Yes, the final state machine represents the state of the object and that the action will trigger the action of the object in the given state.

There are some very good examples on this Wikipedia article.

+2
source

Source: https://habr.com/ru/post/1309934/


All Articles