2016-11-29 68 views
0

我正在嘗試使用Firebase實現具有可觀察設計模式的房間預訂系統。我非常新的可觀察的模式,但我已經創建了一般文件這樣:如何在Android上使用可觀察模式與Firebase

Subject.java

public interface Subject { 
    void Attach(Observer o); 
    void Detach(Observer o); 
    void refreshAccess(); 
} 

Observer.java

public interface Observer { 
    void update(String newAuthorKey); 
} 

Booking.java

public class Booking implements Subject { 
    private String authorKey; //authorKey for the booking 
    private ArrayList<Observer> observers; //users registered for the room 

    public Booking(){ 
     observers = new ArrayList<Observer>(); 
    } 

    public void Attach(Observer o){ 
     observers.add(o); 
    } 

    public void Detach(Observer o){ 
     observers.remove(o); 
    } 

    public void refreshAccess(){ 

    } 

    public void refreshAccess(String newAuthorKey){ 
     this.authorKey = newAuthorKey; 
     this.NotifyObservers(); 
    } 

    private void NotifyObservers(){ 
     for (Observer o:observers) 
      o.update(this.authorKey); 
    } 
} 

User.java

public class User implements Observer { 
    private String authorKey; 
    private Subject subject; 

    public User(Subject subject){ 
     this.subject = subject; 

     //register itself to the subject 
     this.subject.Attach(this); 
    } 

    @Override 
    public void update(String newAuthorKey){ 
     //get update from subject 
     this.authorKey = newAuthorKey; 

     //do something according to the update 
    } 
} 

從本質上講,這個想法是,當一個房間被黃牌警告,新的Booking對象被初始化,並且用戶誰的書它(以及任何其他用戶的他選擇分享此預訂)作爲用戶添加到此預訂中。如何將這些信息更新到Firebase中(我已經在我的應用程序中設置了該信息),以及如何獲得每個用戶訂閱的所有預訂的列表?我在我的android應用程序中有一個處理所有事件監聽器等的片段,但我不知道如何使用Firebase將它連接到這些類。

非常感謝! :)

回答

1

模式Observable可與一個或多個Observer連接的Observer連接。試試這個:

public class Booking extends Observable { 
    private String authorKey; //authorKey for the booking 
    // private ArrayList<Observer> observers; //users registered for the room (no need because Observable holds a list of Observer already) 

    public Booking(String authorKey){ 
     this.authorKey = authorKey; 
    } 


public void refreshAccess(){ 
    nofityObservers(null); 
} 

public void refreshAccess(String newAuthorKey){ 
    String oldAuthorKey = this.authorKey; 
    this.authorKey = newAuthorKey; 
    nofityObservers(oldAuthorKey); 
} 

@Override 
public void notifyObservers(Object arg) { 
    setChanged(); 
    super.notifyObservers(arg); 
} 
} 

public class User implements java.util.Observer { 
private ArrayList<String> authorKeys; // to get all bookings a user has 

public User(){ 
} 

public void addBooking(Booking b) { 
    b.addObserver(this); 
} 

@Override 
public void update(Observable o, Object arg) { 
    if (!(o instanceof Booking)) 
     return; 
    Booking b = (Booking) o; 
    if(arg != null) 
     authorKeys.remove(arg); 
    authorKeys.add(b.getAuthorKey()); 
} 
} 
在您的系統

而且地方:

User u1 = new User(); 
User u2 = new User(); 
Booking b = new Booking(authorKey1); 
b.addObserver(u1); 
b.addObserver(u2); 
b.refreshAccess(); // u1 and u2 get notified 

b.deleteObserver(u2); // you might want to override this to notify user remove booking info 
b.refreshAccess(authorKey2); // u1 get notified 
+0

感謝您的幫助!我會嘗試在我的項目中實現這一點,並告訴你它是如何發生的 –