如何使用 Java 中的 TwoTuple 集合设置变量?

How to set variables using a collection of TwoTuple in Java?

我有一个主要的 class GreenhouseControls,它基本上是一个控制系统,可以处理各种事件,例如 Powerout、FansOn、LightOff、ThermostatNight 等。每个都是单独的事件 classes一个基本的事件,它有一个延迟时间和一个事件时间来显示所有事件的时间间隔。 GreenhouseControls 有各种状态变量来指示事件的状态,例如对于 FansOff 事件,"fans" 变量将为 false。 在我之前的任务中,所有事件 classes 都是 GreenhouseControls 的内部 classes 并且每个 class 都设置了相应的状态变量。
对于此作业,我需要使每个 class 成为一个单独的 class extends Event 和 Event implements Runnable,因此每个事件都是一个线程。
在我们被要求删除所有状态变量并用 TwoTuple 的集合替换它们之后......确切的要求如下: "Remove all existing states variables in GreenhouseControls and replace them by using a collection of TwoTuple. Each time an event runs it should add an entry that identifies the variable the event is modifying as key, and another object to this structure as the value the event is setting. Create a method in GreenhouseControls called setVariable to handle updating to this collection. Use the synchronization feature in java to ensure that two Event classes are not trying to add to the structure at the same time. Again make whatever changes are required to the overall design. Provide adequate methods to access the state variables."

我为此做了以下工作:

首先,我创建了一个基本的双元组class:

public class TwoTuple<A, B> {
    public final A variable;
    public final B value;
    public TwoTuple(A a, B b){variable = a; value = b;}
}

接下来,在 GreenhouseControls 中我有一个集合和一个 setVariable 方法:

public ArrayList<TwoTuple<String,Object>> variables = new    
ArrayList<TwoTuple<String,Object>>();

public synchronized void setVariable(String variable, Object value){
variables.add(new TwoTuple<String,Object>(variable,value));
}

然后,在每个事件的 运行() 方法中,我用一个字符串(这是作为键的变量名称)和该键的值调用 setVariable 方法,例如 LightOff.run() 有以下语句:

gcEvent.setVariable("Light", false);  
//gcEvent is a GreenhouseControls object created in the Event 
//class to access various members of GreenhouseControls.

现在我真的很困惑变量应该放在哪里,因为赋值要求说删除所有状态变量.....在我有: 私人布尔灯=假; //所有class个变量初始化为关闭状态

private boolean water = false;
private boolean fans = false;
private String thermostat = "Day";
private boolean windowok = false;
private boolean poweron = false;

现在,如果我删除它们,那么它们应该在哪里以及如何设置它们? 我不知道在这里做什么.........有人可以花点时间帮助我......任何意见,建议,答案将不胜感激,我需要帮助。 ......如果你理解问题或知道他们在问什么,请回复......谢谢!

就像你对光所做的那样:

gcEvent.setVariable("Light", false);

您需要对其他变量执行相同的操作: 删除:

private boolean water = false;

并做:

 gcEvent.setVariable("water", false);

对所有变量执行此操作。