gpt4 book ai didi

java - 在Java中存储不同类型对象的有序列表的最佳方法?

转载 作者:行者123 更新时间:2023-12-03 21:46:11 25 4
gpt4 key购买 nike

我有几个用于创建对象的类。这些对象每个都有必须根据插入顺序运行的代码。

它们需要按顺序存储 - 并按存储顺序调用。

  • 对象 A
  • 对象 B
  • 对象 A
  • 对象 A
  • 对象 C
  • 对象 D

  • 我不确定在 Java 中处理此问题的正确方法是否是通用列表并在运行时检查输入。
    List<Object> list = new ArrayList<Object>();

    我还考虑过将每个对象类型存储在它们自己的类型列表中,并以正确的顺序从列表中读取父对象。但是,这似乎比仅在运行时检查对象类型更复杂。

    处理此问题的正确“Java”方法是什么?

    最佳答案

    如果对象不共享一个共同的祖先:

    These classes aren't related. They share no common ancestor.



    然后你可以做的是创建另一个类作为包装器:
  • T 类型的对象.
  • 一个 Consumer<T>对象作为对需要调用的代码的引用。

  • 例如:
        class Invocable<T> {
    private final T target;
    private final Consumer<T> invocation;

    public Invocable(T target, Consumer<T> invocation) {
    this.target = target;
    this.invocation = invocation;
    }

    public void runInvocation() {
    invocation.accept(target);
    }
    }

    然后创建另一个管理 List<Invocable> 的类如下所示:
    class RunnableList {

    private List<Invocable<?>> invocables = new ArrayList<Invocable<?>>();

    public <T> void add(T target, Consumer<T> invocation) {
    invocables.add(new Invocable<T>(target, invocation));
    }

    public void run() {
    invocables.forEach(Invocable::runInvocation);
    }
    }

    就是这样!只需将任何对象添加到 RunnableList使用 add(T target, Consumer<T> invocation)方法,当您完成添加所有对象(引用要调用的相应代码)时,只需调用 runRunnableList .

    以下是一个完整的工作示例,请尝试一下以了解这个想法:
    import java.util.ArrayList;
    import java.util.List;
    import java.util.function.Consumer;

    public class RunnableList {

    private List<Invocable<?>> invocables = new ArrayList<Invocable<?>>();

    public <T> void add(T target, Consumer<T> invocation) {
    invocables.add(new Invocable<T>(target, invocation));
    }

    public void run() {
    invocables.forEach(Invocable::runInvocation);
    }

    static class Invocable<T> {
    private final T target;
    private final Consumer<T> invocation;

    public Invocable(T target, Consumer<T> invocation) {
    this.target = target;
    this.invocation = invocation;
    }

    public void runInvocation() {
    invocation.accept(target);
    }
    }

    // TEST

    public static void main(String[] args) {
    RunnableList runnableList = new RunnableList();
    runnableList.add(new ClassA(), o -> o.run1("hello from A1"));
    runnableList.add(new ClassB(), o -> o.run1("hello from B1"));
    runnableList.add(new ClassC(), o -> o.run1("hello from C1"));

    runnableList.add(new ClassA(), ClassA::run2);
    runnableList.add(new ClassB(), ClassB::run2);
    runnableList.add(new ClassC(), ClassC::run2);
    runnableList.run();
    }

    static class ClassA {
    public void run1(String msg) {
    System.out.println("A.run1: " + msg);
    }
    public void run2() { System.out.println("A.run2"); }
    }
    static class ClassB {
    public void run1(String msg) {
    System.out.println("B.run1: " + msg);
    }
    public void run2() { System.out.println("B.run2"); }
    }
    static class ClassC {
    public void run1(String msg) {
    System.out.println("C.run1: " + msg);
    }
    public void run2() { System.out.println("C.run2"); }
    }
    }

    Complete code on GitHub

    希望这可以帮助。

    关于java - 在Java中存储不同类型对象的有序列表的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59119189/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com