2013-03-24 52 views
0

我的類C1擁有所有靜態成員(變量和方法)。它還有一個可觀察的C2類型對象的數組A1。我希望C1成爲一個觀察者,注意到C2的變化,然後通過使用A1中包含的引用將這些變化應用於對象。 我正在使用C1的一個實例,以便它可以添加爲C2對象的觀察者。Observer observable design pattern - 用靜態成員創建一個類Observer?

觀察者可觀察的設計模式是否適合這種情況?如果是,那麼我是否正確應用?

import java.util.*; 

class FlightStatusChangeEvent{ 

int status; 
Flight flight; 

FlightStatusChangeEvent(int statusCode, Flight flight){ 

    this.status = statusCode; 
    this.flight = flight; 

} 

public int getStatus(){return this.status;} 

public Flight getFlight(){return this.flight;} 


} 

class Flight extends Observable{ 

int status;// 0 = on time, -1 = late, +1 = before scheduled time 

public void updateStatus(int statusCode){ 


    this.status = statusCode; 
    FlightStatusChangeEvent fsce = new FlightStatusChangeEvent(status, this); 
    setChanged(); 
    notifyObservers(fsce); 

} 


} 

class FlightStatusMonitor implements Observer{ 

public static ArrayList<Flight> flights = new ArrayList<Flight>(); 

//keep calling this method every 10 sec to get latest flight status 
public void displayStatusOfFlights(){ 

    //print all flight statuses in array list - flights 
} 

public void update(Observable flight, Object flightStatusEvent){ 

    FlightStatusChangeEvent fsce = (FlightStatusChangeEvent) flightStatusEvent;  
    Flight fl = fsce.getFlight(); 
    int status = fsce.getStatus(); 

    //find the flight in array flights and then update its status 

} 


} 
+1

你能分享一些代碼?而且所有靜態成員類都是一個很好的反模式。你能改變它嗎? – DiogoSantana 2013-03-24 02:45:26

+0

@DiogoSantana - 我添加了代碼。 – 2013-03-24 03:08:27

+0

爲什麼FlightStatusMonitor.flights需要是靜態的?沒有這個,你可以做你想做的事。 – DiogoSantana 2013-03-24 03:11:45

回答

0

這是怎麼Observer模式可以在你的情況下,可以實現:

class FlightStatusEvent { 

    int status; 
    Flight flight; 

    FlightStatusEvent(int statusCode, Flight flight) { 
     this.status = statusCode; 
     this.flight = flight; 
    } 

    public int getStatus() { 
     return this.status; 
    } 

    public Flight getFlight() { 
     return this.flight; 
    } 

} 

class Flight extends Observable { 

    int status;// 0 = on time, -1 = late, +1 = before scheduled time 

    public void updateStatus(int statusCode) { 

     this.status = statusCode; 
     FlightStatusEvent fsce = new FlightStatusEvent(status, this); 
     setChanged(); 
     notifyObservers(fsce); 

    } 

} 

class FlightStatusMonitor implements Observer { 

    public ArrayList<Flight> flights = new ArrayList<Flight>(); 

    public void displayStatusOfFlights() { 
     for(Flight f : flights) { 
      System.out.println("Flight " + f + ", status = " + f.status); 
     } 
    } 

    public void update(Observable flight, Object flightStatusEvent) { 
     displayStatusOfFlights(); 
    } 

    public static void main(String[] args) { 
     FlightStatusMonitor fsm = new FlightStatusMonitor(); 

     Flight f1 = new Flight(); 
     f1.addObserver(fsm); 
     Flight f2 = new Flight(); 
     f2.addObserver(fsm); 

     ArrayList<Flight> flights = new ArrayList<Flight>(); 
     flights.add(f1); 
     flights.add(f2); 

     fsm.flights = flights; 
     fsm.displayStatusOfFlights(); 

     // this will trigger observer pattern event. 
     f1.updateStatus(1); 
    } 

} 
+0

Param爲什麼你改變我的靜態成員代碼給班級成員?對於靜態類,O-O模式是否失敗? – 2013-03-27 09:31:03

+0

理論上,靜態字段/方法屬於類(而不是對象)。在你的情況下,航班對象屬於FSM對象,而不是類本身。 O-O模式可以同時使用靜態/非靜態方法/字段。 – Param 2013-03-27 17:46:01