论坛首页 Java企业应用论坛

DesignPattern学习-----Observer

浏览 1776 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-08-28  
    Observer直译为中文为观察者。所以此模式又称为观察者模式(废话。)不过一直很疑惑,我感觉这个名字起得不好,给人误解很大,特别是初学者。我想大部分人的想法应该和我一样,我第一次看到观察者模式就会想到,观察者观察者,那么肯定观察者是主体了。其实不然。被观察者才是主体。
     就小孩睡觉这个例子,如果按观察者的想法,那就观察嘛!!!怎么观察,小孩在睡觉,我就在那观察,他眼一睁,啊,要喝奶了。赶紧去泡奶粉。。。。这个显然就不正常。傻子才这么干。。。。
     实际上是你该干嘛就干嘛去,小孩醒了,一哭要喝奶了,你就去泡奶吧。
     所以我认为观察者模式叫Call模式或者Call-Response模式更恰当。
     下面看程序,模拟找工作。。。。雇主通知他要招聘的雇员,雇员得到通知后做出响应。
     先是两个接口,Employer和Employee。
package pig.pattern.Observer;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2008-8-28
 * Time: 14:31:33
 * To change this template use File | Settings | File Templates.
 */
public interface Employer {
    void agree_call();           
    void agree(Employee employee);  //相当于swing里的add***Listener()
}


package pig.pattern.Observer;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2008-8-28
 * Time: 14:32:14
 * To change this template use File | Settings | File Templates.
 */
public interface Employee {
    void response(EmployEvent event);
//这个相当于ActiongListener里面的actionPerformed(ActionEvent event)
}

再来相应的实现类
package pig.pattern.Observer;

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

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2008-8-28
 * Time: 14:32:51
 * To change this template use File | Settings | File Templates.
 */
public class Employer_1 implements Employer{
    private List<Employee> employees = new ArrayList<Employee>();
    private EmployEvent event;

    public void agree(Employee employee){
        employees.add(employee);
    }

    public void agree_call() {
        event = new EmployEvent();
        event.setEmployer_name(this.getClass().getSimpleName());
        for(Employee e : employees){
            e.response(event);
        }
    }
}

package pig.pattern.Observer;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2008-8-28
 * Time: 14:49:22
 * To change this template use File | Settings | File Templates.
 */
public class Employee_1 implements Employee{
    public void response(EmployEvent event) {
        System.out.println("Oh,I'm lucky.I'm employed by " + event.getEmployer_name());
    }
}

package pig.pattern.Observer;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2008-8-28
 * Time: 14:49:22
 * To change this template use File | Settings | File Templates.
 */
public class Employee_2 implements Employee{
    public void response(EmployEvent event) {
        System.out.println("Yeah,I'm employed by " + event.getEmployer_name());
    }
}


再来事件类
package pig.pattern.Observer;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2008-8-28
 * Time: 14:39:48
 * To change this template use File | Settings | File Templates.
 */
public class EmployEvent {
    private String employer_name;

    public String getEmployer_name() {
        return employer_name;
    }

    public void setEmployer_name(String employer_name) {
        this.employer_name = employer_name;
    }
}

最后测试一下。
package pig.pattern.Observer;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2008-8-28
 * Time: 14:51:00
 * To change this template use File | Settings | File Templates.
 */
public class Test {
    public static void main(String[] args){
        Employer e_1 = new Employer_1();
        e_1.agree(new Employee_1());
        e_1.agree(new Employee_2());
        e_1.agree_call();
    }
}

Employer在众多的简历里面挑选出自己满意的,然后agree到自己的通知列中(list),全部都确认后,然后一起去call所有的这些应聘者,各个应聘者做出不同的反应。EmployEvent就相当于通知书一样,里面保存了Employer的名称,不然你怎么知道你是被哪个Employer聘了?
这还是说明,观察者(Employee)并不是主体,被观察者(Employer)才是主体。如果Employee是主体,那是不是你投完简历以后就天天跟着那个Employer,他一天不做出决定,你就一天不停了?
至于用到哪儿,根据代码应该就能猜得出来。一个对象的改变会影响其他对象的响应,而且对象个数不确定。
很粗糙的代码,只想说别被名字唬到了。。。。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics