zno2

20) Observer pattern

类别:

 Behavioral Pattern

问题:

 

方案1:

 

 

示例1:

import java.util.ArrayList;
import java.util.List;

public class ObserverPatternDemo {
    public static void main(String[] args) {
        Subject subject = new Subject();
        new HexaObserver(subject);
        new OctalObserver(subject);
        new BinaryObserver(subject);
        System.out.println("First state change: 15");
        subject.setState(15);
        System.out.println("Second state change: 10");
        subject.setState(10);
    }
}

class Subject {
    private List<Observer> observerList = new ArrayList<Observer>();
    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        notifyAllObservers();
    }

    public void attach(Observer observer) {
        observerList.add(observer);
    }

    public void notifyAllObservers() {
        for (Observer observer : observerList) {
            observer.update();
        }
    }
}

abstract class Observer {
    protected Subject subject;

    public abstract void update();
}

class BinaryObserver extends Observer {
    public BinaryObserver(Subject subject) {
        this.subject = subject;
        this.subject.attach(this);
    }

    @Override
    public void update() {
        System.out.println("Binary String: " + Integer.toBinaryString(subject.getState()));
    }
}

class OctalObserver extends Observer {
    public OctalObserver(Subject subject) {
        this.subject = subject;
        this.subject.attach(this);
    }

    @Override
    public void update() {
        System.out.println("Octal String: " + Integer.toOctalString(subject.getState()));
    }
}

class HexaObserver extends Observer {
    public HexaObserver(Subject subject) {
        this.subject = subject;
        this.subject.attach(this);
    }

    @Override
    public void update() {
        System.out.println("Hex String: " + Integer.toHexString(subject.getState()).toUpperCase());
    }
}

 

First state change: 15
Hex String: F
Octal String: 17
Binary String: 1111
Second state change: 10
Hex String: A
Octal String: 12
Binary String: 1010

示例2 使用java.util:

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

public class ObserverPatternDemo {
    public static void main(String[] args) {
        Subject subject = new Subject();
        subject.addObserver(new BinaryObserver());
        subject.addObserver(new OctalObserver());
        subject.addObserver(new HexaObserver());

        System.out.println("First state change: 15");
        subject.setState(15);
        System.out.println("Second state change: 10");
        subject.setState(10);
    }
}

class Subject extends Observable {

    public synchronized void setState(int state) {
        super.setChanged();
        super.notifyObservers(state);
    }
}

class BinaryObserver implements Observer {

    public void update(Observable o, Object arg) {
        System.out.println("Binary String: " + Integer.toBinaryString((Integer) arg));
    }

}

class OctalObserver implements Observer {

    public void update(Observable o, Object arg) {
        System.out.println("Octal String: " + Integer.toOctalString((Integer) arg));
    }

}

class HexaObserver implements Observer {

    public void update(Observable o, Object arg) {
        System.out.println("Hex String: " + Integer.toHexString((Integer) arg));
    }

}
First state change: 15
Hex String: f
Octal String: 17
Binary String: 1111
Second state change: 10
Hex String: a
Octal String: 12
Binary String: 1010

 

应用:

 

不足:(

 

优化:)

 

posted on 2023-06-09 13:36  zno2  阅读(14)  评论(0编辑  收藏  举报

导航