所以我有一个有点奠定了作为一个数据类:接口提取一个布尔值或字符串
class MyData {
String str1,str2,str3;
Boolean bool1,bool2;
}
的属性将被填充基于字符串输入,是这样的:
public void populate(String s) {
if(s.contains("somevalue") myData.setStr1("xxx");
if(s.constains("something else") myData.setBool1(true);
else myData.setBool1(false);
}
这一点,当然,一个非常可怕的方式做事情s.contains
实际上是一些漂亮的毛茸茸的条件,所以不是我定义的接口:
public interface DataFinderInterface {
public String findStringData(final String input);
public Boolean findBooleanData(final String input);
}
因此populate方法可以被重写为:
public void populate(String s) {
myData.setStr1(str1Finder.findStringData(s));
myData.setBool1(bool1Finder.findBooleanData(s);
}
这个接口的任一定义findStringData或findBooleanData,这是相当不令人满意的实现。填充方法需要知道我们是否期望使用findStringData方法或findBooleanData方法。
有没有更好的方法来做到这一点?我是过于挑剔,因为填充方法需要知道什么样的DataFinderInterface实例分配到哪个字段呢?
我不知道这是否只有我,但你的问题不是很清楚。这看起来像[XY问题](http://meta.stackexchange.com/q/66377/196975)。可能你应该先解释你的问题陈述。你想要做什么,然后你现在拥有什么。 –
填充也需要能够设置'str2','str3'和'bool2'? –