zoukankan      html  css  js  c++  java
  • java之观察者模式

    import java.util.Observable;
    import java.util.Observer;

    class House extends Observable {
        private float price;

        public float getPrice() {
            return price;
        }

        public void setPrice(float price) {
            super.setChanged();
            super.notifyObservers(price);
            this.price = price;
        }

        public String toString() {
            return "House [price=" + price + "]";
        }

        public House(float price) {
            this.price = price;
        }
    }

    class HousePriceObserver implements Observer {
        private String nameString;

        public HousePriceObserver(String name) {
            this.nameString = name;
        }

        @Override
        public void update(Observable o, Object arg) {
            if (arg instanceof Float) {
                System.out.print(this.nameString + "price changed to be: ");
                System.out.println(((Float) arg).floatValue());
            }

        }
    }

    public class ObserDemo01 {
        public static void main(String[] args) {
            House house = new House(100000);
            HousePriceObserver hPriceObserver01 = new HousePriceObserver("A");
            HousePriceObserver hPriceObserver02 = new HousePriceObserver("B");
            HousePriceObserver hPriceObserver03 = new HousePriceObserver("B");
            house.addObserver(hPriceObserver01);
            house.addObserver(hPriceObserver02);
            house.addObserver(hPriceObserver03);
            System.out.println(house);
            house.setPrice(594030900);
            System.out.println(house);
        }
    }

  • 相关阅读:
    android图片特效处理之光晕效果
    Android 给图片加边框
    Android学习笔记进阶十三获得本地全部照片
    startActivityForResult()的用法
    Android学习笔记进阶十二之裁截图片
    Android学习笔记进阶十一图片动画播放(AnimationDrawable)
    Android控件开发之Gallery3D效果
    android中图片倒影、圆角效果重绘
    两种方法求最大公约数最小公倍数
    Nginx 负载均衡配置和策略
  • 原文地址:https://www.cnblogs.com/vonk/p/3912686.html
Copyright © 2011-2022 走看看