设计模式学习----观察者模式

【观察者模式解释】

 

类型:行为模式

 

定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象的状态发生改变时,会通知所有的观察者对象,使它们能够自己更新自己。

 

implements Java.util.Observer
extends Java.util.Observable

 

新建抽象发布通知父类:

 

package carer_subject;

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

import carer_observer.carer_observer;

public interface carer_subject_parent {

    public void set_state(String state_string);

    public String get_state();

    public void add_observer(carer_observer carer_observer_ref);

    public void sub_observer(carer_observer carer_observer_ref);

    public void notityAllCarer();

}

 

新建发布通知实现类:

 

package carer_subject;

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

import carer_observer.carer_observer;

public class carer_subject_sub implements carer_subject_parent {

    List<carer_observer> list_observer = new ArrayList<carer_observer>();

    private String state;

    public void set_state(String state_string) {
        this.state = state_string;
    }

    public String get_state() {
        return state;
    }

    public void add_observer(carer_observer carer_observer_ref) {
        list_observer.add(carer_observer_ref);
    }

    public void sub_observer(carer_observer carer_observer_ref) {
        list_observer.remove(carer_observer_ref);

    }

    public void notityAllCarer() {
        state = "到达终点";
        for (int i = 0; i < list_observer.size(); i++) {
            list_observer.get(i).show_info();
        }

    }

}

 

新建观察者接口:

 

package carer_observer;

import carer_subject.carer_subject_parent;

public interface carer_observer {

    public void show_info();

}

 

新建观察者实现类:

 

package carer_observer;

import carer_subject.carer_subject_parent;

public class carer_observer_imple implements carer_observer {

    private carer_subject_parent carer_subject_parent_ref;

    public carer_subject_parent getCarer_subject_parent_ref() {
        return carer_subject_parent_ref;
    }

    public void setCarer_subject_parent_ref(
            carer_subject_parent carer_subject_parent_ref) {
        this.carer_subject_parent_ref = carer_subject_parent_ref;
    }

    public carer_observer_imple(carer_subject_parent carer_subject_parent_ref) {
        super();
        this.carer_subject_parent_ref = carer_subject_parent_ref;
    }

    public void show_info() {
        System.out.println(carer_subject_parent_ref.get_state());

    }

}

 

新建客户端运行类:

 

package run_main;

import carer_observer.carer_observer_imple;
import carer_subject.carer_subject_parent;
import carer_subject.carer_subject_sub;

public class run_main {

    public static void main(String[] args) {

        carer_subject_parent carer_subject_parent_ref = new carer_subject_sub();

        carer_observer_imple carer_observer_imple_ref1 = new carer_observer_imple(
                carer_subject_parent_ref);
        carer_observer_imple carer_observer_imple_ref2 = new carer_observer_imple(
                carer_subject_parent_ref);
        carer_observer_imple carer_observer_imple_ref3 = new carer_observer_imple(
                carer_subject_parent_ref);
        carer_observer_imple carer_observer_imple_ref4 = new carer_observer_imple(
                carer_subject_parent_ref);

        carer_subject_parent_ref.add_observer(carer_observer_imple_ref1);
        carer_subject_parent_ref.add_observer(carer_observer_imple_ref2);
        carer_subject_parent_ref.add_observer(carer_observer_imple_ref3);
        carer_subject_parent_ref.add_observer(carer_observer_imple_ref4);

        carer_subject_parent_ref.sub_observer(carer_observer_imple_ref3);

        carer_subject_parent_ref.set_state("到达终点");

        carer_subject_parent_ref.notityAllCarer();

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值