javaSE (27) (관찰자 디자인 모드)

1.package us.google.www;

import java.util.Vector;

/** *           * @author chongrubujing * */
public class Doll implements Subject{
    //                 (             )
    private Vector<Observer> v = new Vector<Observer>();
    private float price;

    public Doll(float price) {
        super();
        this.price = price;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        //     ,        
        this.price = price;
        notifyObserver();
    }

    @Override
    public void registerObserver(Observer o) {
        //     
        v.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        //     
        v.remove(o);
    }

    @Override
    public void notifyObserver() {
        //          
        for (Observer o : v) {
            o.update(price);
        }
    }

}
2.package us.google.www;
/** *       * @author chongrubujing * */
public interface Observer {
    /** *            */
    public void update(float price);
}
3.package us.google.www;
/** *          * @author chongrubujing * */
public class Person implements Observer {
    private String name;

    public Person(String name) {
        super();
        this.name = name;
    }

    @Override
    public void update(float price) {
        System.out.println(name+"              :"+price);
    }

}
4.package us.google.www;


/** *        * @author chongrubujing * */
public interface Subject {
    /** *          * @param o */
    public void registerObserver(Observer o);
    /** *       * @param o */
    public void removeObserver(Observer o);
    /** *      (    ) * @param o */
    public void notifyObserver();
}
5.package us.google.www;

public class Test {

    public static void main(String[] args) {
        //          
        Doll doll = new Doll(3000);
        //         
        Person p1 = new Person("  ");
        Person p2 = new Person("   ");
        //       
        doll.registerObserver(p1);
        doll.registerObserver(p2);
        System.out.println("     :");
        //    
        doll.setPrice(2888);
        System.out.println("     :");
        doll.setPrice(2599);
        System.out.println("     :");
        doll.setPrice(1988);
        doll.removeObserver(p2);
        System.out.println("     :");
        doll.setPrice(1599);


    }

}
6.package us.google.observer2;
/** *     obseverable             */
import java.util.Observable;


public class Doll extends Observable{
    private float price;

    public Doll(float price) {
        super();
        this.price = price;
    }

    public float getPrice() {
        return price;
    }

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

}
7.package us.google.observer2;

import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.SynchronousQueue;

/* * *     java.util.Observer             */
public class Person implements Observer{
    private String name;
    public Person(String name) {
        this.name = name;
    }


    @Override
    public void update(Observable o, Object arg) {
        if(o instanceof Observable)
        {
            Doll  doll = (Doll)o;
            System.out.println(name+"            :"+doll.getPrice());
        }
    }

}
8.package us.google.observer2;

public class Test {

    public static void main(String[] args) {
        Doll doll = new Doll(3000);
        Person p1 = new Person("  ");
        Person p2 = new Person("  ");
        doll.addObserver(p1);
        doll.addObserver(p2);
        System.out.println("     ");
        doll.setPrice(2988);
        System.out.println("     ");
        doll.setPrice(2698);
        System.out.println("     ");
        doll.setPrice(1998);
    }

}

좋은 웹페이지 즐겨찾기