论坛首页 Java企业应用论坛

我工作的那点事--学习《设计模式》实例应用(Mediator模式)

浏览 2231 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-01-24   最后修改:2010-02-22
    最近因为保险上面问题,为了核实工作的是否为自己交保险,然后自己亲自和各保险部门打交道,这才发现:1.中国政府部门真的很多 2.时间效率真的很慢。
原来只要把这些交给公司办理,不用管这么多。

    最近在学习设计模式,突然想到 公司在无意中扮演了Mediator角色,为了学以致用,将写在下面,可能这方面已经有很多人写了。

1.创意员工类:
public class Colleague {
private Mediator mediator;

public Colleague() {
}
/***/
/**
* 构造函数
*/
public Colleague(Mediator mediator) {
this.mediator = mediator;
}

/** */
/**
* 获得调停者对象
*/
public Mediator getMediator() {
return mediator;
}

public void createAccount(){
mediator.createAccount();
}

/*
* 提供员工存入功能
* */
public void saveInsurance(){
mediator.saveInsurance();
}

/*
* 提供员工取消功能
* */
public void cancelInsurance(){
mediator.cancelInsurance();
}

/*
* 提供员工更改功能
* */
public void changedInsurance(){
mediator.changedInsurance();
}

/*
* 提供员工查询功能
* */
public void detailInsurance(){
mediator.detailInsurance();
}
}


2.公司类:
这里作为mediator角色,用户交互个人与部门的关系。
public abstract class Mediator {
public abstract void colleagueChanged(Colleague colleage);

/*
* 提供员工建立帐户功能
* */
public abstract void createAccount();

/*
* 提供员工存入功能
* */
public abstract void saveInsurance();

/*
* 提供员工取消功能
* */
public abstract void cancelInsurance();

/*
* 提供员工更改功能
* */
public abstract void changedInsurance();

/*
* 提供员工查询功能
* */
public abstract void detailInsurance();

/*
* 传达政府部门信息 通知给员工
* */
public abstract void notify(Colleague colleage);
}

具体实现的类:
public class ConcreteMediator extends Mediator {
private LaborInsurance laborInsurance;
private MedicalInsurance medicalInsurance;
private EduInsurance eduInsurance;
private UnemploymentInsurance unemplInsurance;
private BearingInsurance bearingInsurance;

private HouseFund houseFund;

public ConcreteMediator(){
super();
createConcreteMediator();
}

public void colleagueChanged(Colleague colleague) {

}

/** */
/**
* 工厂方法,创建部门对象
*/
public void createConcreteMediator() {
laborInsurance = new LaborInsurance();
medicalInsurance = new MedicalInsurance();
eduInsurance = new EduInsurance();
unemplInsurance = new UnemploymentInsurance();
bearingInsurance = new BearingInsurance();
houseFund = new HouseFund();
}

/**
* 公司与政府部门交涉处理开户功能
*/
public void createAccount(){
laborInsurance.createAccount();
medicalInsurance.createAccount();
eduInsurance.createAccount();
unemplInsurance.createAccount();
bearingInsurance.createAccount();
houseFund.createAccount();
}

/**
* 公司与政府部门交涉处理存入功能
*/
public void saveInsurance() {
laborInsurance.saveInsurance();
medicalInsurance.saveInsurance();
eduInsurance.saveInsurance();
unemplInsurance.saveInsurance();
bearingInsurance.saveInsurance();
houseFund.saveInsurance();
}

/**
* 公司与政府部门交涉处理销户功能
*/
public void cancelInsurance() {
laborInsurance.cancelInsurance();
medicalInsurance.cancelInsurance();
eduInsurance.cancelInsurance();
unemplInsurance.cancelInsurance();
bearingInsurance.cancelInsurance();
houseFund.cancelInsurance();
}

/**
* 公司与政府部门交涉处理更改功能
*/
public void changedInsurance() {
laborInsurance.changedInsurance();
medicalInsurance.changedInsurance();
eduInsurance.changedInsurance();
unemplInsurance.changedInsurance();
bearingInsurance.changedInsurance();
houseFund.changedInsurance();
}

/**
* 公司与政府部门交涉处理查询功能
*/
public void detailInsurance() {
laborInsurance.detailInsurance();
medicalInsurance.detailInsurance();
eduInsurance.detailInsurance();
unemplInsurance.detailInsurance();
bearingInsurance.detailInsurance();
houseFund.detailInsurance();
}

/**
* 公司将政府部门信息传递给用户
*/
public void notify(Colleague colleage){

}
}


3.政府部门:
LaborInsurance laborInsurance; 劳动保险部门
MedicalInsurance medicalInsurance;医疗保险部门
EduInsurance eduInsurance; 教育金部门
UnemploymentInsurance unemplInsurance; 失业保险部门
BearingInsurance bearingInsurance; 生育保险部门
HouseFund houseFund; 住房公积金部门
public class LaborInsurance {
private Mediator mediator;

public Mediator getMediator(){
return mediator;
}

public void setMediator(Mediator mediator){
this.mediator = mediator;
}

/*
*保险部分通知客户
* */
public void notifyColleague(){
Colleague colleague = new Colleague();
mediator.notify(colleague);
}

public void createAccount(){
System.out.println("you are in LaborInsurance and creating account^_^");
}

public void saveInsurance() {
System.out.println("you are in LaborInsurance and saving^_^");
}

public void cancelInsurance() {
System.out.println("you are in LaborInsurance and cancelInsurance ^_^");
}

public void changedInsurance() {
System.out.println("you are in LaborInsurance and changedInsurance ^_^");
}

public void detailInsurance() {
System.out.println("you are in LaborInsurance and detailInsurance ^_^");
}
}

其他部门类同上。

4.员工交保险的过程测试 如下:
public void testMediator() {

Mediator mediator = new ConcreteMediator();
Colleague colleague = new Colleague(mediator);
colleague.createAccount();
colleague.saveInsurance();
colleague.changedInsurance();
colleague.detailInsurance();
colleague.cancelInsurance();
}


这就是整个过程,简单的理解了mediator.

感觉有点代理的意思,不过个人理解代理也是有实现功能的 只是处理的另一种方法,而mediator只是中间器没有具体实现功能,没有权利的。
论坛首页 Java企业应用版

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