设计模式综合-会议管理系统的实现
(1)案例背景:
某会议管理系统的“会议通知发送”模块说明如下:
(1)行政管理人员可以给某个或者某些员工(Employee)发送会议通知,也可以给某个部门
(Department)发送通知,如果给某个部门发送通知,将逐个给该部门每个员工发送会议通知(2)如果员工或者部门希望能够收到会议通知,必须先注册到一个会议列表(MeetingList)中,在发送通知时,系统将遍历会议列表,逐个将会议通知发送给注册用户(User)
(2)实现步骤:
- 根据题意,选用合适的两种设计模式
- 根据这两种设计模式,画出本题的类图
- 根据类图,实现“会议通知发送”模块的代码(附加题,会根据实现情况加平时成绩)
- 设计模式的选用需要根据具体要求来进行
- 多种设计模式可以进行混合使用
(4)案例总结:
Ø 混合使用时,需要根据选用的不同设计模式,对类图进行修改
两种模式:组合模式,观察者模式
代码结构:
详细代码:
MyElement.java:
package notice.Element; public abstract class MyElement { public abstract void send(); }
Employee.java:
package notice.Element; import notice.Observer.MyObserver; public class Employee extends MyElement implements MyObserver { public void send(){ System.out.println("发送文件给员工!"); } }
Department.java:
package notice.Element; import notice.Observer.MyObserver; import java.util.ArrayList; public class Department extends MyElement implements MyObserver { private ArrayList list = new ArrayList(); public void add(MyElement element){ list.add(element); } public void remove(MyElement element){ list.remove(element); } public void send(){ for (Object object:list){ ( (MyElement)object ).send(); } } }
MySubject.java:
package notice.Observer; import java.util.ArrayList; public abstract class MySubject { public ArrayList meetingList = new ArrayList(); public void attach(MyObserver observer){ meetingList.add(observer); } public void detach(MyObserver observer){ meetingList.remove(observer); } public abstract void regist(); }
MyObserver.java:
package notice.Observer; public interface MyObserver { void send(); }
User.java:
package notice.Observer; import notice.Element.Department; import notice.Element.Employee; import notice.Element.MyElement; public class User extends MySubject{ public void regist(){ MyElement emp1,emp2,emp3; Department dep1; emp1 = new Employee(); emp2 = new Employee(); emp3 = new Employee(); dep1 = new Department(); dep1.add(emp1); dep1.add(emp2); meetingList.add(emp3); meetingList.add(dep1); for (Object obs:meetingList){ System.out.print("员工注册信息! "); ((MyElement)obs).send(); } } }
Client.java:
package notice.Client; import com.sun.org.apache.bcel.internal.generic.MONITORENTER; import notice.Element.Department; import notice.Element.Employee; import notice.Element.MyElement; import notice.Observer.MyObserver; import notice.Observer.MySubject; import notice.Observer.User; public class Client { public static void main(String[] args) { System.out.println("软件1904班 白旭君 2019005368"); MySubject subject = new User(); MyObserver obs1,obs2; obs1 = new Employee(); obs2 = new Department(); subject.attach(obs1); subject.attach(obs2); subject.regist(); // MyElement emp1,emp2,emp3; // Department dep1,dep2; // // emp1 = new Employee(); // emp2 = new Employee(); // emp3 = new Employee(); // dep1 = new Department(); // dep2 = new Department(); // // dep1.add(emp1); // dep1.add(emp2); // // dep2.add(dep1); // dep2.add(emp3); // // dep2.send(); } }
实验结果: