Guido Muscioni
Guido Muscioni

Reputation: 1295

Fill an object without knowing the real type of the object

I have a problem in my code: i have some classes that implements an interface like this:

public interface MyInterface{
puclic void myMethod{...}
}

public class Class1 implements MyInterface{
private int choice1;
private String choice2;
(Override of the other method define in interface)
}

public class Class2 implements MyInterface{
private String choice1;
private int choice2;
(Override of the other method define in interface)
}

These classes are in my model, I would like to ask the view to select individually each of the parameters in these classes and save the choiches from view one to one in the model, but i have to use my controller to verify the input. The problem is that in the controller i don't know the type of the object so i can't call the specific method get or set. Moreover i can't write it in the interface because i have different parameters in Class1 and Class2. So i don't know how to save the choiche from view in the model.

Upvotes: 0

Views: 143

Answers (1)

J Fabian Meier
J Fabian Meier

Reputation: 35805

Put the save logic inside Class1 and Class2. Let them save their values, or at least let them produce some kind of String, List etc. that the view can save being agnostic of the concrete object. Summarized, I propose to add a method save() to the Interface which either saves the state or returns the state in some savable manner.

Upvotes: 2

Related Questions