Observer pattern [2]

package observerDM;

import java.util.Observable;

public class ConcreteSubject extends Observable {

        public ConcreteSubject(String _name , float _price) {
                this.name = _name;
                this.price = _price;
                System.out.println("ConcreteSubject created:" + _name + "at" + _price);
        }

        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
                setChanged();
                notifyObservers(name);
        }
        public float getPrice() {
                return price;
        }
        public void setPrice(float price) {
                this.price = price;
                setChanged();
                notifyObservers(price);
        }

        private String name ;
        private float price ;
}
package observerDM;

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

public class NameObserver implements Observer {

        public NameObserver() {
                this.name = null ;
                System.out.println("NameObserver has created:Name is :" + this.name);
        }

        public void update(Observable o, Object arg) {

                if(arg instanceof String) {
                        name = (String)arg;
                        System.out.println("NameObserver has changed:Name is :" + name);
                }else {
                        System.out.println("NameObserver :some other change to subject");
                }
        }

        private String name ;
}
package observerDM;

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

public class PriceObserver implements Observer {

        public PriceObserver() {
                this.price = 0 ;
                System.out.println("PriceObserver has created : Price is :" + price);
        }

        public void update(Observable o, Object arg) {
                if(arg instanceof Float) {
                        price = ((Float)arg).floatValue();
                        System.out.println("PriceObserver has changed:Price is :" + price);
                }else {
                        System.out.println("PriceObserver :some other change to subject");
                }
        }

        private float price ;
}
package observerDM;

public class TestObservers {

        public static void main(String[] args){

                ConcreteSubject sub = new ConcreteSubject("Com pops",(float)1.30);

                NameObserver name = new NameObserver();
                PriceObserver price = new PriceObserver();

                sub.addObserver(name);
                sub.addObserver(price);

                sub.setName("Frosted Flakes");
                sub.setPrice((float)1.40);

                sub.setPrice((float)2.03);
                sub.setName("helloWorld");
        }
}

Observer pattern [2]

分类:Java 时间:2010-08-24 人气:167
分享到:
blog comments powered by Disqus

相关文章

  • java.util.Date and java.sql.Date system conversion and converted to a date time format string 2010-11-26

    1, will be converted to a java.sql.Date java.util.Date java.sql.Date sd; java.util.Date ud; / / Initialize the ud such as ud = new java.util.Date (); sd = new java.sql.Date (ud.getTime ()); 2, to insert into the database and the corresponding field f

  • Bash string manipulation (control and Java) - 4 string output 2011-09-08

    Bash string manipulation (control and Java) - 4 string output In Java Output to the standard output device (console, screen) System.out.println (s); Output to the standard error device (the console screen) System.err.println (s); Output to a file Pri

  • Bash string manipulation (control and Java) - 6 to determine whether a string is empty (not empty) 2011-09-14

    Bash string manipulation (control and Java) - 6 to determine whether a string is empty (not empty) In Java Determine whether the null Is null if (s == null) { / / Do something } Not null if (s! = null) { / / Do something } Determine whether the null

  • Bash string manipulation (control and Java) - 5 string input (read string) 2011-09-13

    Bash string manipulation (control and Java) - 5 string input (read string) In Java Scanner class: Scanner.hasNext () & Scanner.next () & Scanner.hasNextLine () & Scanner.nextLine () JavaDoc class Scanner wrote A simple text scanner which can p

  • String class methods in Java and description (a) 2010-10-19

    String: string type First, the constructor String (byte [] bytes): string object through the byte array constructor. String (char [] value): String object constructed by an array of char. String (Sting original): Constructs a copy of the original. Ie

  • String class methods in Java and that (b) 2010-10-19

    Java String class in the common methods: public char charAt (int index) Back to index a string of characters; public int length () Returns the string length; public int indexOf (String str) Returns the string position of first occurrence of str; publ

  • java applet web server automatically reports the public network IP address 2010-04-05

    Companies take a SVN server, because there is no fixed IP address. Note the IP to the company e-mail serve, and come home to connect to the server through this IP, IP not found. So one way to write a small program, which the server automatically dete

  • In a JAVA source file can only define a public class 2011-05-03

    java's argument passed by value only, without reference. In a JAVA source file can only define a public class First.java file public class First { public static void main (String [] args) { System.out.println ("first class"); } } public class Se

  • Observer pattern java 2010-03-29

    Observer pattern: As the name suggests is a personal observation some things, once these things changed, the observer can get to know the situation. Just as today's spy movies follow the same, the boss at home command, observe the dynamic brother on

  • Java Design Patterns - Observer Pattern (Observer pattern) 2010-12-14

    Java Design Patterns - Observer Pattern (Observer pattern) Articles Category: Java Programming Keywords: observer mode observer pattern Observer pattern using the more in software programming, so good with words I use the Internet to see the story, a

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.821 (s). 10 q(s)