我正在创建一个空中交通管制系统。我有一个叫做plane的类,它是一个被观察到的任何改变,这个改变是随机生成一个平面时名称改变的。问题是机场需要访问名称以将名称存储到数组列表中,如果我可以将其存储到数组列表中,则可以使其工作。反正这是机场类的代码:Java - 观察模式 - 观察一个班级,但可以访问另一个班级正在观察的内容
/*
* 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这是目前的是什么主题指出,但是它有随机数字和字母出来产生:
[email protected]
这是我在我的主题类:
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);
}
}
}
任何人都可以点我在正确的方向如何从荷航取得状态这一点出来,因为荷航正好出现,但它不会在机场班出来。
任何建议将大受欢迎。
您将'SubjectImpl @ 5a199939'看作'System.out.println(s)'的输出的原因在于''SubjectImpl'类中的toString()方法没有被覆盖但除此之外,我不确定你在问什么。 – darrengorman 2012-04-07 10:30:17