基于输入类型进行决策

huangapple 未分类评论57阅读模式
英文:

Making decisions based on kind of input

问题

public class Main {
    public static void main(String[] args) {
        DataStructure s = new DataStructure();
        Input in = new Input();
        Data x = null;
        while ((x = in.readNext()) != null) {
            x.performAction(s);
        }
    }
}

interface Data {
    void performAction(DataStructure s);
}

class InsertData implements Data {
    // Fields and constructor for InsertData

    @Override
    public void performAction(DataStructure s) {
        s.insert(this);
    }
}

class QueryData implements Data {
    // Fields and constructor for QueryData

    @Override
    public void performAction(DataStructure s) {
        System.out.println(s.query(this));
    }
}

class DataStructure {
    // Implementation of DataStructure class
}

class Input {
    public Data readNext() {
        // Implementation of reading user input and returning Data
    }
}

请注意,上述代码是对您原始代码的改进,以避免使用 instanceof 和条件判断来执行不同操作。在这个版本中,我使用了接口和方法重写来实现多态性,从而更优雅地处理不同类型的操作。

英文:
public class Main {
    public static void main(String[] args) {
        DataStructure s = new DataStructure();
        Input in = new Input();
        Data x = null;
        while ((x = in.readNext()) != null) {
            if (x instanceof InsertData) {
                s.insert((InsertData)x);
            } else if (x instanceof QueryData) {
                System.out.println(s.query((QueryData)x));
            }
        }
    }
}

Input reads user input and returns it in the form of Data using method Data readNext().

Data can be InsertData or QueryData (they also have different fields, i.e. form of data to insert is different than query).

What I'd like to do is read Data and perform insert/query on DataStructure s, depending on what type of Data was returned by readNext().

Code above is my simple solution for this problem, but it uses conversion and instanceof which isn't a great coding style. Can you suggest any better solution?

Another solution I thought of would be to have a method void performAction(DataStructure s) in Data which would be overriden by InsertData/QueryData and perfom corresponding action on DataStructure, but I think it would unnecessarily bind DataStructure with Data, which wouldn't be a good style either. Meaning, I'd like to keep input and performing actions apart, because reading data and doing something with it are kinda different things.

答案1

得分: 0

Add method performAction(Data) to class DataStructure.
Then add abstract method performOperation() to class Data.
Override method performOperation() in class InsertData and class QueryData.
Method performAction(Data) in class DataStructure would be as follows:

public void performAction(Data x) {
    x.performOperation();
}

and your while loop (in your method main()) would be:

while ((x = in.readNext()) != null) {
   s.performAction(x);
}
英文:

Add method performAction(Data) to class DataStructure.
Then add abstract method performOperation() to class Data.
Override method performOperation() in class InsertData and class QueryData.
Method performAction(Data) in class DataStructure would be as follows:

public void performAction(Data x) {
    x.performOperation();
}

and your while loop (in your method main()) would be:

while ((x = in.readNext()) != null) {
   s.performAction(x);
}

答案2

得分: 0

你可以做的是在DataStructure类中添加一个通用的process(Data data)方法,根据实例类型插入或查询数据。这样,DataStructure类负责决策,而不是Data类。

如果你正在使用Java 14,你也可以摆脱显式转换(参见https://www.baeldung.com/java-pattern-matching-instanceof)。

英文:

What you could do is add a generic process(Data data) method in the DataStructure class, that inserts or queries the data depending on the instance type. This way, the DataStructure class is responsible for the decision making, instead of theData class.

If you're using Java 14, you could get rid of the explicit cast as well (see https://www.baeldung.com/java-pattern-matching-instanceof).

huangapple
  • 本文由 发表于 2020年5月2日 17:51:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/61557395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定