1 java设计模式中有一个模式:观察者模式。观察者模式是首先注册需要被观察的对象,然后如果发生变化,会通知每个注册的比观察者。简单来说就是主动通知需要通知的类。
2 观察者模式。是先将被观察者添加到观察者的list属性中。然后当观察者发生变化时,主动通知这些被观察者对象。
3 代码如下:
public interface Beiguanchazhe {
public void update(String state);
}
public interface Guanchazhe {
public void addRegister(Beiguanchazhe beiguanchazhe);
public void reomveRegister(Beiguanchazhe beiguanchazhe);
public void tongzhi(String state);
}
public class MeigeBeiguanchazhe implements Beiguanchazhe{
private String state=null;
public MeigeBeiguanchazhe() {
super();
// TODO Auto-generated constructor stub
}
public void update(String state) {
// TODO Auto-generated method stub
this.state=state;
System.out.println(this+"状态已改为:"+this.state);
}
}
public class MeigeGuanchazhe implements Guanchazhe{
private List<Beiguanchazhe> list=new ArrayList<Beiguanchazhe>();
public MeigeGuanchazhe() {
super();
// TODO Auto-generated constructor stub
}
public void addRegister(Beiguanchazhe beiguanchazhe) {
// TODO Auto-generated method stub
this.list.add(beiguanchazhe);
}
public void reomveRegister(Beiguanchazhe beiguanchazhe) {
// TODO Auto-generated method stub
this.list.remove(beiguanchazhe);
}
public void tongzhi(String state) {
// TODO Auto-generated method stub
for(int i=0;i<this.list.size();i++){
Beiguanchazhe beiguanchazhe=this.list.get(i);
beiguanchazhe.update(state);
}
}
}
public static void main(String[] args) {
Beiguanchazhe b1=new MeigeBeiguanchazhe();
Beiguanchazhe b2=new MeigeBeiguanchazhe();
Guanchazhe guanchazhe=new MeigeGuanchazhe();
guanchazhe.addRegister(b1);
guanchazhe.addRegister(b2);
guanchazhe.tongzhi("1");
}
!!!综述:观察者模式(关键字:注册、通知)
2 观察者模式。是先将被观察者添加到观察者的list属性中。然后当观察者发生变化时,主动通知这些被观察者对象。
3 代码如下:
public interface Beiguanchazhe {
public void update(String state);
}
public interface Guanchazhe {
public void addRegister(Beiguanchazhe beiguanchazhe);
public void reomveRegister(Beiguanchazhe beiguanchazhe);
public void tongzhi(String state);
}
public class MeigeBeiguanchazhe implements Beiguanchazhe{
private String state=null;
public MeigeBeiguanchazhe() {
super();
// TODO Auto-generated constructor stub
}
public void update(String state) {
// TODO Auto-generated method stub
this.state=state;
System.out.println(this+"状态已改为:"+this.state);
}
}
public class MeigeGuanchazhe implements Guanchazhe{
private List<Beiguanchazhe> list=new ArrayList<Beiguanchazhe>();
public MeigeGuanchazhe() {
super();
// TODO Auto-generated constructor stub
}
public void addRegister(Beiguanchazhe beiguanchazhe) {
// TODO Auto-generated method stub
this.list.add(beiguanchazhe);
}
public void reomveRegister(Beiguanchazhe beiguanchazhe) {
// TODO Auto-generated method stub
this.list.remove(beiguanchazhe);
}
public void tongzhi(String state) {
// TODO Auto-generated method stub
for(int i=0;i<this.list.size();i++){
Beiguanchazhe beiguanchazhe=this.list.get(i);
beiguanchazhe.update(state);
}
}
}
public static void main(String[] args) {
Beiguanchazhe b1=new MeigeBeiguanchazhe();
Beiguanchazhe b2=new MeigeBeiguanchazhe();
Guanchazhe guanchazhe=new MeigeGuanchazhe();
guanchazhe.addRegister(b1);
guanchazhe.addRegister(b2);
guanchazhe.tongzhi("1");
}
!!!综述:观察者模式(关键字:注册、通知)