《研磨设计模式》chap10 中介者模式Mediator(2)应用举例
生活随笔
收集整理的這篇文章主要介紹了
《研磨设计模式》chap10 中介者模式Mediator(2)应用举例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
人員類和部門類之間的關系:刪除一個人或一個部門時,需要同時刪除人員類和部門類里的數據,增加操作也是如此。
public class User{ private String userId; //用戶編號 private String userName;//人員離職 public boolean dimission(){//1:要先通過中介者去清除掉所有與這個人員相關的部門和人員的關系DepUserMediatorImpl mediator = DepUserMediatorImpl.getInstance();mediator.deleteUser(userId);return true;} }public class Dep{private String depId; //描述部門編號 private String depName; //描述部門名稱 //撤銷部門 public boolean deleteDep(){//1:要先通過中介者去清除掉所有與這個部門相關的部門和人員的關系DepUserMediatorImpl mediator = DepUserMediatorImpl.getInstance();mediator.deleteDep(depId); }public class DepUserModel { private String depUserId; //用戶編號 private String depId;//描述部門編號 }public class DepUserMediatorImpl{private static DepUserMediatorImpl mediator = new DepUserMediatorImpl();public boolean deleteDep(String depId) { //1:到記錄部門和人員關系的集合里面,尋找跟這個部門相關的人員//設置一個臨時的集合,記錄需要清除的關系對象Collection<DepUserModel> tempCol = new ArrayList<DepUserModel>();for(DepUserModel du : depUserCol){if(du.getDepId().equals(depId)){//2:需要把這個相關的記錄去掉,先記錄下來tempCol.add(du);}}//3:從關系集合里面清除掉這些關系depUserCol.removeAll(tempCol);public boolean deleteUser(String userId) {//1:到記錄部門和人員關系的集合里面,尋找跟這個人員相關的部門//設置一個臨時的集合,記錄需要清除的關系對象Collection<DepUserModel> tempCol = new ArrayList<DepUserModel>();for(DepUserModel du : depUserCol){if(du.getUserId().equals(userId)){//2:需要把這個相關的記錄去掉,先記錄下來tempCol.add(du);}}//3:從關系集合里面清除掉這些關系depUserCol.removeAll(tempCol); }總結
以上是生活随笔為你收集整理的《研磨设计模式》chap10 中介者模式Mediator(2)应用举例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《研磨设计模式》chap10 中介者模式
- 下一篇: 《研磨设计模式》chap19 备忘录模式