熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

java 設計模式之 觀察者模式(Observer)

2022-06-13   來源: Java高級技術 

  //Subjectjava

  package comyoungmastermodelObserver;

  /**

  * @author youngmaster

  * @Email:young * @version myEclipse

  * @create time上午::

  */

  /**

  * 察者模式屬於行為型模式其意圖是定義對象間的一種一對多的依賴關系

  * 當一個對象的狀態發生改變時所有依賴於它的對象都得到通知並被自動更新

  * 在制作系統的過程中將一個系統分割成一系列相互協作的類有一個常見的副作用

  * 需要維護相關對象間的一致性我們不希望為了維持一致性而使各類緊密耦合

  * 因為這樣降低了他們的可充用性這一個模式的關鍵對象是目標(Subject)和觀察者(Observer)

  * 一個目標可以有任意數目的依賴它的觀察者一旦目標的狀態發生改變所有的觀察者都得到通知

  * 作為對這個通知的響應每個觀察者都將查詢目標以使其狀態與目標的狀態同步這種交互也稱為發布訂閱模式

  * 目標是通知的發布者他發出通知時並不需要知道誰是他的觀察者可以有任意數據的觀察者訂閱並接收通知

  */

  /**

  * subject

  *目標接口

  */

  public interface Subject {

  public void addObserver(Observer o);

  public void deletObserver(Observer o);

  public void notice();

  }

  //Observerjava

  package comyoungmastermodelObserver;

  /**

  * @author youngmaster

  * @Email:young * @version myEclipse

  * @create time上午::

  */

  /**

  *觀察者接口

  */

  public interface Observer {

  public void update();

  }

  //Teacherjava

  package comyoungmastermodelObserver;

  import javautilVector;

  /**

  * @author youngmaster

  * @Email:young * @version myEclipse

  * @create time上午::

  */

  /**

  *目標接口實現

  */

  public class Teacher implements Subject {

  private String phone;

  @SuppressWarnings(unchecked)

  private Vector students;

  @SuppressWarnings(unchecked)

  public Teacher() {

  phone = ;

  students = new Vector();

  }

  @SuppressWarnings(unchecked)

  @Override

  public void addObserver(Observer o) {

  studentsadd(o);

  }

  @Override

  public void deletObserver(Observer o) {

  studentsremove(o);

  }

  @Override

  public void notice() {

  for (int i = ; i < studentssize(); i++) { ((Observer) studentsget(i))update();

  }

  }

  public void setPhone(String phone) {

  thisphone = phone;

  notice();

  }

  public String getPhone() {

  return phone;

  }

  }

  //Studentjava

  package comyoungmastermodelObserver;

  /**

  * @author youngmaster

  * @Email:young * @version myEclipse

  * @create time上午::

  */

  /**

  *觀察者接口實現

  */

  public class Student implements Observer {

  private String name;

  private String phone;

  private Teacher teacher;

  public Student(String name Teacher teacher) { thisname = name; thisteacher = teacher;

  }

  public void show() {

  Systemoutprintln(Name: + name + \nTeachers phone: + phone);

  }

  @Override

  public void update() {

  phone = teachergetPhone();

  }

  }

  //Clientjava

  package comyoungmastermodelObserver;

  import javautilVector;

  /**

  * @author youngmaster

  * @Email:young * @version myEclipse

  * @create time上午::

  */

  /**

  *測試類

  */

  public class Client {

  /**

  * @param args

  */

  @SuppressWarnings(unchecked)

  public static void main(String[] args) {

  Vector students = new Vector();

  Teacher teacher = new Teacher();

  for (int i = ; i < ; i++) {

  Student student = new Student(student + i teacher); studentsadd(student); teacheraddObserver(student);

  }

  teachersetPhone();

  for (int i = ; i < ; i++)

  ((Student) studentsget(i))show();

  Systemoutprintln(\n============================\n);

  teachersetPhone();

  for (int i = ; i < ; i++)

  ((Student) studentsget(i))show();

  }

  }


From:http://tw.wingwit.com/Article/program/Java/gj/201311/27566.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.