import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Swing 观察者模式
*/
public class Swings extends JButton {
@Override
public String toString() {
return "Swings{}";
}
public static void main(String[] args) {
Swings swings = new Swings(); //被观察者-主题
swings.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("观察者:"+e.getActionCommand() + e.getID() + e.getSource());
}
}); // 注册订阅者
swings.fireActionPerformed(new ActionEvent(swings, 100, "[被观察者-触发事件]")); //被观察者-通知观察者(主题-通知-订阅者)
}
}