我正在创建一个空中交通管制系统。我有一个名为 plane 的类,它可以观察到任何变化,即随机生成飞机时名称会发生变化。问题是机场需要访问该名称以将其存储到一个数组列表中,如果我可以让它工作的话,可能是一个队列。无论如何,这是机场类的代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package airtrafficcontrolv3;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author
*/
public class Airport
{
Subject s = new SubjectImpl();
Plane point = new Plane();
Queue <String> waiting = new LinkedList<String>();
public Airport()
{
//String name =
s.getState();
System.out.println(s);
while (!waiting.isEmpty())
{
System.out.println("Waiting to take off: "+waiting);
try
{
System.out.println("Preparing to taxi: "+waiting.remove());
Thread.sleep(5000);
}
catch (InterruptedException ex)
{
Logger.getLogger(Airport.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
我不确定我是否必须访问正在观察的主题,因为在主题中我有一个 getstate 方法,这是当前所指向的,但是它以随机数字和字母生成:
airtrafficcontrolv3.SubjectImpl@5a199939
这是我在我的学科课上的:
package airtrafficcontrolv3;
/*
* @author S0082708
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
interface Subject
{
public void addObserver(Observer o);
public void removeObserver(Observer o);
public String getState();
public void setState(String state);
}
/*
* Creates an observer that will watch for any changes
*/
interface Observer
{
public void update(Subject o);
}
/*
* Class implements the observer
* Observer is what is doing the watching
* The string is set to blank waiting ready for any updates that will occur
* Anything in getState is then passed to state
*/
class ObserverImpl implements Observer
{
private String state = "";
public void update(Subject o)
{
state = o.getState();
//Need to add text to the label
// AirTrafficControlv3View.Incoming.add(state);
}
}
/*
* Subject is what is being watched
* The array is then created to store the information ready to add further information
* Or ready to delete
* State is set to blank ready for whatever information is being passed
*/
class SubjectImpl implements Subject
{
private List observers = new ArrayList();
private String state = "";
/*
* Returns whatever is being passed to state
*/
public String getState()
{
//System.out.println(state);
return state;
}
/*
* Sets the state to current state and then notifies the observer of the changes
* Made to the state ready to update
*/
public void setState(String state)
{
this.state = state;
notifyObservers();
}
/*
* Adds the observer along with the name, so several observers can be implemented
*/
public void addObserver(Observer o)
{
observers.add(o);
}
/*
* Removes the observer once it has been used
*/
public void removeObserver(Observer o)
{
observers.remove(o);
}
/*
* The iterator allows the ability to loop through the array
* While the iterator has a next then it allows the ability to take in more
* Changes to be observed. It is then updated with the current information
*/
public void notifyObservers()
{
Iterator i = observers.iterator();
while (i.hasNext())
{
Observer o = (Observer) i.next();
o.update(this);
}
}
}
谁能指出我正确的方向,即如何从 get state 中获取字符串,该字符串以 KLM 的形式出现,但它不会在机场类中出现。
任何建议都将受到欢迎。