浏览 2231 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-01-24
最后修改:2010-02-22
原来只要把这些交给公司办理,不用管这么多。 最近在学习设计模式,突然想到 公司在无意中扮演了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只是中间器没有具体实现功能,没有权利的。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |