设计模式之观察者模式(ObserverMethod)

观察者模式的主题是观察者,所有的被观察者作为list中的一个key被传入,观察者观察每次操作的是哪个对象,通过只这些对该对象的进行影响。

代码如下:

package ObserverMethod;


import java.util.Vector;


public interface Subject {
public abstract void attach(Observer o);
    public abstract void detach(Observer o);
    public abstract void sendNotify();


    public abstract Vector getState();
    public abstract void setState(String act, String str);
}


package ObserverMethod;


import java.util.*;
import java.io.*;


public class ConcreteSubject implements Subject {
    private LinkedList observerList;
    private Vector strVector;
    
    public ConcreteSubject() {
        observerList =  new LinkedList();
        strVector = new Vector();
    }
    public void attach(Observer o) {
        observerList.add(o);
    }
    public void detach(Observer o) {
        observerList.remove(o);
    }
    public void sendNotify() {
        for(int i = 0; i < observerList.size(); i++) {
            ((Observer)observerList.get(i)).update(this);   
        }
    }
    public void setState(String act, String str) {
        if(act.equals("ADD")) {
            strVector.add(str);
        } else if(act.equals("DEL")) {
            strVector.remove(str);
        }
    }
    public Vector getState() {
        return strVector;
    }
}


package ObserverMethod;


public class Test {
public static void main(String[] args) {
        Subject mySub = new ConcreteSubject();
        ObserverA myObserverA = new ObserverA(mySub);
        ObserverB myObserverB = new ObserverB();
        mySub.attach(myObserverA);
        mySub.attach(myObserverB);


        mySub.setState("ADD", "One --- 1");
        mySub.setState("ADD", "Tow --- 2");
        mySub.sendNotify();


        myObserverA.change("DEL", "Tow --- 2");
        myObserverA.change("ADD", "Three --- 3");
        myObserverA.change("ADD", "Four --- 4");
        myObserverA.notifySub();  
        
        mySub.sendNotify();
    }
}


package ObserverMethod;


public interface Observer {
public void update(Subject s);
}


package ObserverMethod;


import java.util.Vector;


public class ObserverA implements Observer {
    private Vector strVector;
    private Subject sub;
    public ObserverA(Subject s) {
        sub = s;
        //strVector = new Vector();
    }
    public void update(Subject subject) {
        strVector = subject.getState();
        System.out.println("----- ObserverA will be updated -----");
        for(int i = 0; i < strVector.size(); i++) {
            System.out.println("Num " + i + " is :" + (String)strVector.get(i));
        }
    }
    public void change(String action, String str) {
        sub.setState(action, str);
        //we can auto update
        //sub.sendNotify();
    }
    public void notifySub() {
        sub.sendNotify();
    }
}


package ObserverMethod;


import java.io.*;
import java.util.*;


public class ObserverB implements Observer {
    private Vector strVector;
    public ObserverB() {
        strVector =  new Vector();
    }
    public void update(Subject subject) {
        strVector = (Vector)(subject.getState()).clone();
        //-----  Sorted vector  ---------------------------
        for (int i = strVector.size(); --i >= 0; ) {
            for (int j = 0; j < i; j++) {
                String str1 = (String)strVector.get(j);
                String str2 = (String)strVector.get(j+1);
                if((str1.compareTo(str2)) > 0) {
                    strVector.setElementAt(str2, j);
                    strVector.setElementAt(str1, j+1);
                }
            }
        }
        System.out.println("----- ObserverB will be updated -----");
        for(int i = 0; i < strVector.size(); i++) {
            System.out.println("Num " + i + " is :" + (String)strVector.get(i));
        }
    }
    
}


输出:

----- ObserverA will be updated -----
Num 0 is :One --- 1
Num 1 is :Tow --- 2
----- ObserverB will be updated -----
Num 0 is :One --- 1
Num 1 is :Tow --- 2
----- ObserverA will be updated -----
Num 0 is :One --- 1
Num 1 is :Three --- 3
Num 2 is :Four --- 4
----- ObserverB will be updated -----
Num 0 is :Four --- 4
Num 1 is :One --- 1
Num 2 is :Three --- 3
----- ObserverA will be updated -----
Num 0 is :One --- 1
Num 1 is :Three --- 3
Num 2 is :Four --- 4
----- ObserverB will be updated -----
Num 0 is :Four --- 4
Num 1 is :One --- 1
Num 2 is :Three --- 3

发布了428 篇原创文章 · 获赞 195 · 访问量 81万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览