设计模式之观察者模式
程序员文章站
2022-05-03 10:29:24
...
其实大多数设计模式里面都有多态的体现,观察这模式其实 就是人与人之间一样,观察一个人状态的变化,然后做出相应的处理。
package com.lyc.design;
import java.util.ArrayList;
import java.util.List;
public class Observer {
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<Observer1> observers = new ArrayList<Observer1>();
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
//改变后调用相应的方法
notifyAllObservers();
}
public void attach(Observer1 observer) {
observers.add(observer);
}
public void notifyAllObservers() {
for (Observer1 observer : observers) {
observer.update();
}
}
}
abstract class Observer1 {
protected Subject subject;
public abstract void update();
}
class BinaryObserver extends Observer1 {
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 Observer1 {
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 Observer1 {
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());
}
}
package com.lyc.design;
import java.util.ArrayList;
import java.util.List;
public class Observer {
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<Observer1> observers = new ArrayList<Observer1>();
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
//改变后调用相应的方法
notifyAllObservers();
}
public void attach(Observer1 observer) {
observers.add(observer);
}
public void notifyAllObservers() {
for (Observer1 observer : observers) {
observer.update();
}
}
}
abstract class Observer1 {
protected Subject subject;
public abstract void update();
}
class BinaryObserver extends Observer1 {
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 Observer1 {
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 Observer1 {
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());
}
}