ݺߣ

ݺߣShare a Scribd company logo
     



All Rights Reserved
? ۲ģʽһһԶϵ
  ö۲߶ͬʱijһ
  ״̬Ϸ仯ʱ
  ֪ͨй۲߶ܹԶ
  Լ
? ۲ģʽ
 C ɫжԹ۲߶ñ
   һУÿɫ
   Ĺ۲ߡṩһӿڣ
   Ӻɾ۲߽ɫһһ
   ӿʵ֡
 C ۲߽ɫΪоĹ۲߶һ
   ӿڣڵõ֪ͨʱԼ
? ۲ģʽ
 C ɫھڲ״̬ıʱ
   еǼǹĹ۲߷֪ͨ
   ɫͨһʵ֡
 C ۲߽ɫýɫʵֳ۲߽ɫ
   Ҫĸ½ӿڣԱʹ״̬
   ״̬ЭҪ۲߽ɫ
   Աһָɫáͨ
   һʵ
? ʵԼĹ۲ģʽ
? ۲ģʽJavaеĵλҪ
? JDKҲṩ˶Թ۲ģʽ֧
? ObservableڴԹ۲⵽ij
  ֵࡣĶ
  仯ʱ۲౻֪ͨ۲ʵ
  ֶupdate( )Observerӿڡ
  һ۲֪ͨһ۲
  ıʱupdate( )á
? һ۲Ķ򵥹
  һıˣsetChanged( )
  ڶ׼֪ͨ۲ĸıʱ
  notifyObservers( )⵼ڹ
  жupdate( )ĵáע⡪
  ڵnotifyObservers( )֮ǰûе
  setChanged( )Ͳʲô
  update( )֮ǰ۲
  setChanged( )notifyObservers( )ַ
? עnotifyObservers()ʽһִ
  вһûСò
  notifyObservers( )ʱö󱻴
  update( )Ϊڶ
  򣬽update( )һnull
  ʹõڶʺӦó
  κ͵Ķ
? Ϊ˹۲һɹ۲Ķ󣬱ʵ
  Observerӿڡӿڽ
  ʾһ
 C void update(Observable observOb,
   Object arg)
 C observObDZ۲Ķ󣬶arg
   notifyObservers( )ݵֵ۲
   ˸ı䣬update( )
? һ˵ɹ۲ӡó򴴽һ
  Watcher࣬ʵObserverӿڡص
  BeingWatched̳ObservableBeingWatched
  counter( )÷Ǵһֵָʼݼ
  ÿμıʱnotifyObservers( )ã
  ǰļΪݸnotifyObservers( )
  Watcherеupdate( )ãʾǰļ
  ֵmain( )ڣֱobservingobservedWatcher
  BeingWatched󱻴Ȼobservingӵ
  observedĹ۲бζÿcounter( )
  notifyObservers( )ʱobserving.update( )
? μ ObserverDemo.java
? ж۲
  ʵ۲ಢҽÿе
  һӵBeingWatched۲б
  Сڶ۲ȴֱΪ0
? μ TwoObservers.java

More Related Content

[ʥ˼԰][Java SE]Observer

  • 1. All Rights Reserved
  • 2. ? ۲ģʽһһԶϵ ö۲߶ͬʱijһ ״̬Ϸ仯ʱ ֪ͨй۲߶ܹԶ Լ
  • 3. ? ۲ģʽ C ɫжԹ۲߶ñ һУÿɫ Ĺ۲ߡṩһӿڣ Ӻɾ۲߽ɫһһ ӿʵ֡ C ۲߽ɫΪоĹ۲߶һ ӿڣڵõ֪ͨʱԼ
  • 4. ? ۲ģʽ C ɫھڲ״̬ıʱ еǼǹĹ۲߷֪ͨ ɫͨһʵ֡ C ۲߽ɫýɫʵֳ۲߽ɫ Ҫĸ½ӿڣԱʹ״̬ ״̬ЭҪ۲߽ɫ Աһָɫáͨ һʵ
  • 7. ? ObservableڴԹ۲⵽ij ֵࡣĶ 仯ʱ۲౻֪ͨ۲ʵ ֶupdate( )Observerӿڡ һ۲֪ͨһ۲ ıʱupdate( )á
  • 8. ? һ۲Ķ򵥹 һıˣsetChanged( ) ڶ׼֪ͨ۲ĸıʱ notifyObservers( )⵼ڹ жupdate( )ĵáע⡪ ڵnotifyObservers( )֮ǰûе setChanged( )Ͳʲô update( )֮ǰ۲ setChanged( )notifyObservers( )ַ
  • 9. ? עnotifyObservers()ʽһִ вһûСò notifyObservers( )ʱö󱻴 update( )Ϊڶ 򣬽update( )һnull ʹõڶʺӦó κ͵Ķ
  • 10. ? Ϊ˹۲һɹ۲Ķ󣬱ʵ Observerӿڡӿڽ ʾһ C void update(Observable observOb, Object arg) C observObDZ۲Ķ󣬶arg notifyObservers( )ݵֵ۲ ˸ı䣬update( )
  • 11. ? һ˵ɹ۲ӡó򴴽һ Watcher࣬ʵObserverӿڡص BeingWatched̳ObservableBeingWatched counter( )÷Ǵһֵָʼݼ ÿμıʱnotifyObservers( )ã ǰļΪݸnotifyObservers( ) Watcherеupdate( )ãʾǰļ ֵmain( )ڣֱobservingobservedWatcher BeingWatched󱻴Ȼobservingӵ observedĹ۲бζÿcounter( ) notifyObservers( )ʱobserving.update( )
  • 13. ? ж۲ ʵ۲ಢҽÿе һӵBeingWatched۲б Сڶ۲ȴֱΪ0