Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:独立的主逻辑&;异常处理逻辑_Java_Design Patterns_Refactoring - Fatal编程技术网

Java:独立的主逻辑&;异常处理逻辑

Java:独立的主逻辑&;异常处理逻辑,java,design-patterns,refactoring,Java,Design Patterns,Refactoring,问题:如何将所有逻辑移动到策略(另一个类),但将异常处理逻辑保留在类所有者中 我得到了:方法map,它从输入参数中获取数据数组,然后对其执行大量逻辑: class Owner ... public void map(final LongWritable key, final Text value, final Context context) { // 1st-fragment of code String[] data = null; try { // Not Own

问题:如何将所有逻辑移动到策略(另一个类),但将异常处理逻辑保留在类所有者中


我得到了:方法
map
,它从输入参数中获取
数据
数组,然后对其执行大量逻辑:

class Owner ...
public void map(final LongWritable key, final Text value, final Context context) {
    // 1st-fragment of code
    String[] data = null;
    try { // Not Owner class logic, should be moved to strategy
        StringReader reader = new StringReader(line);
        CustomParser cParser = new CustomParser(reader,_strategy);
        data = cParser.getLine();
    } catch (final IOException e) {
        outputBadData(line); // Owner class logic
        return;
    }

    // 2nd-fragment of code: logic which is based on `data` array
    ................
    ................
}
我想要:实际上
1st
片段中的所有逻辑(除了
outputBadData
)都不属于此类。我想改变它的策略。它看起来像:

public void map(final LongWritable key, final Text value, final Context context) {
    // 1st-fragment of code
    String[] data = strategy.getData(value);

    // 2nd-fragment of code: logic which is based on `data` array
    ................
    ................
}

问题:
outputBadData
Owner
类的逻辑,而不是策略。

如果我理解正确,您必须创建策略类,并在其中定义具有所需逻辑的方法。 在此方法的签名中定义“抛出IOException”

例如:

public [return type] readLine(arguments...) throws IOException {
    ...
}
此外,您必须具有对新类(策略)的对象的引用才能调用“readLine”方法。 创建此类实例时,可以将此对象存储为所有者类的字段,并将其作为构造函数参数传递(用于使用setter方法)。然后,在Owner类的“map”方法中,用“try catch”块围绕“readLine”方法调用来处理异常:

try{
     strategyReference.readLine(arguments...); 

    }catch (IOException e) {
        outputBadData(line); 
        return;

问题在于
strategyReference.readLine
抛出异常并返回值。即使它抛出异常,我们也应该继续处理异常之前收集的数据。在这种情况下,在“try-catch”blok中的“readLine()”方法中处理“IOException”可能是更好的方法。在“try-catch”中,您可以使用BadData初始化“data”数组。在异常处理之后,您可以从此方法返回数组。“map()”方法将在任何情况下获取数据。但下一个问题是:正确和错误数据的后续逻辑是否相同?也许你下一步要进行数据验证?