我工作的那點事--學習《設計模式》例項應用(Mediator模式)

biqing0427發表於2007-11-20

最近在學習設計模式,突然想到 公司在無意中扮演了Mediator角色

[@more@]最近和公司因為保險上面問題 差生些矛盾,為了核實工作的是否為自己交保險,然後自己親自和各保險部門打交道,這才發現: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只是中間器沒有具體實現功能,沒有權利的。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9879276/viewspace-983083/,如需轉載,請註明出處,否則將追究法律責任。

相關文章