Java Common Class Library--observer design pattern (observable class observer interface)

Source: Internet
Author: User

Original link: http://www.2cto.com/kf/201310/253013.html

If you want to implement the observer pattern, you must rely on the observable class and observer interfaces provided in the Java.util package.

123456789101112131415161718192021222324252627282930313233343536373839404142434445 import java.util.* ;  class House extends Observable{ // 表示房子可以被观察      private float price ;// 价钱      public House(float price){          this.price = price ;          public float getPrice(){          return this.price ;          public void setPrice(float price){          // 每一次修改的时候都应该引起观察者的注意          super.setChanged() ;    // 设置变化点          super.notifyObservers(price) ;// 价格被改变          this.price = price ;          public String toString(){          return "房子价格为:" + this.price ;      };   class HousePriceObserver implements Observer{      private String name ;      public HousePriceObserver(String name){ // 设置每一个购房者的名字          this.name = name ;          public void update(Observable o,Object arg){          if(arg instanceof Float){              System.out.print(this.name + "观察到价格更改为:") ;              System.out.println(((Float)arg).floatValue()) ;              };  public class ObserDemo01{      public static void main(String args[]){          House h = new House(1000000) ;          HousePriceObserver hpo1 = new HousePriceObserver("购房者A") ;          HousePriceObserver hpo2 = new HousePriceObserver("购房者B") ;          HousePriceObserver hpo3 = new HousePriceObserver("购房者C") ;          h.addObserver(hpo1) ;          h.addObserver(hpo2) ;          h.addObserver(hpo3) ;          System.out.println(h) ; // 输出房子价格          h.setPrice(666666) ;    // 修改房子价格          System.out.println(h) ; // 输出房子价格      };

Running Result: House price is: 1000000.0 Buyers C observed price changed to: 666666.0 House Buyer B observed price changed to: 666666.0 buyer a observed price changed to: 666666.0 houses Price: 666666.0

Java Common Class Library--observer design pattern (observable class observer interface)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.