Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7070702
  • 博文数量: 703
  • 博客积分: 10821
  • 博客等级: 上将
  • 技术积分: 12041
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-02 10:41
个人简介

中科院云平台架构师,专注于数字化、智能化,技术方向:云、Linux内核、AI、MES/ERP/CRM/OA、物联网、传感器、大数据、ML、微服务。

文章分类

全部博文(703)

分类: Java

2013-12-06 15:48:16

 

明:察者察行,添加被象到察列表中。
1
利用JDK

package hiya.observer.jdk;

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

// this is a subject察者:察行的人)
public class BeingWatched extends Observable{
    void doBusiness() {
        for (int period = 5; period >= 0; period--) {
            if (true) {
                super.setChanged();
            }
            notifyObservers( new Integer ( period ) );
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println("Sleep interrupeted");
            }
        }
    }

    public static void main(String [] args) {
        //
建一个察者 chf@tsinghua.org.cn
        BeingWatched subject = new BeingWatched();

        //建两个被察者
        Observer mailObserver = new MailObserver();
        Observer countObserver = new CountObserver();

        //把两个被察者加到察者列表要察的象列表中
        subject.addObserver(mailObserver);
        subject.addObserver(countObserver);

        //业务操作
        subject.doBusiness();
    }
}

package hiya.observer.jdk;

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

public class CountObserver implements Observer {

    @Override
    public void update(Observable o, Object arg) {
        System.out.println("Update() called, count is " + ((Integer) arg).intValue());
    }

}

package hiya.observer.jdk;

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

public class MailObserver implements Observer {

    @Override
    public void update(Observable o, Object arg) {
        System.out.println("You is sending mail!....OK");
    }

}

果:
Update() called, count is 5
You is sending mail!....OK
Update() called, count is 4
You is sending mail!....OK
Update() called, count is 3
You is sending mail!....OK
Update() called, count is 2
You is sending mail!....OK
Update() called, count is 1
You is sending mail!....OK
Update() called, count is 0
You is sending mail!....OK

较简单容易理解.
以下是自己写的:(1)用 2)大部分相同,只修改不一的地方

package hiya.observer;

public class ObserverPatternTest {

    /**
     * [
メソッドの説明を書きましょう]
     * @param args
     */
    public static void main(String[] args) {

        Subject subject1 = new Subject();
        Subject subject2 = new Subject();

        subject1.addPaymentListener(new Observer(){
            public String Action(SubjectEvent subjectEvent){
                return subjectEvent.getText() + " with cash. ";
            }
        });

        subject2.addPaymentListener(new Observer(){
            public String Action(SubjectEvent subjectEvent){
                return subjectEvent.getText() + " with credit. ";
            }
        });

        subject1.setAmount(777);
        subject2.setAmount(888);

        System.out.println(subject1.sellAction());
        System.out.println(subject2.sellAction());

    }

}


 

package hiya.observer;

import java.util.EventListener;

public interface Observer extends EventListener{
   
    public String Action(SubjectEvent subjectEvent);

}

 

package hiya.observer;

import java.util.ArrayList;

public class Subject {
    private double amount;
   
    private ArrayList eventListenerLst;
   
    public void addPaymentListener(Observer observer){
        if (eventListenerLst == null){
            eventListenerLst = new ArrayList();
        }
       
        if (!eventListenerLst.contains(observer)){
            eventListenerLst.add(observer);
           
        }
    }
   
    public void removePaymentListener(Observer observer){
        if (eventListenerLst != null && eventListenerLst.contains(observer)){
            eventListenerLst.remove(observer);
        }
    }
   
    public String sellAction(){
        String sellAction = null;
       
        String preSellAction = "You has payment ";
        sellAction = preSellAction;

        sellAction += amount;
        SubjectEvent subjectEvent = new SubjectEvent(this, sellAction);
       
        sellAction = activeEvent(subjectEvent);
       
        sellAction += " I am selecting warehouse, Please waiting...";
       
        return sellAction;
    }

    private String activeEvent(SubjectEvent subjectEvent) {
       
        //String text = subjectEvent.getText();
        String text = "";
        if (eventListenerLst != null){
           
            for (int i=0; i< eventListenerLst.size(); i++){
                Observer observer = (Observer)eventListenerLst.get(i);
                text += observer.Action(subjectEvent);
            }
           
        }
        return text;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

}

 

package hiya.observer;

import java.util.EventObject;

public class SubjectEvent extends EventObject {
   
    private String text;

    // http://hiyachen.blog.chinaunix.net
    public SubjectEvent(Object source, String text) {
        super(source);
        setText(text);
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    } 
}

You has payment 777.0 with cash.  I am selecting warehouse, Please waiting...
You has payment 888.0 with credit.  I am selecting warehouse, Please waiting...



 

阅读(1916) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~