我正在創建一個空中交通管制系統。我有一個叫做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