publicclassTestObserver { publicstaticvoidmain(String[] args) { // 初始化 Paperpaper=newPaper(); Tomtom=newTom(); Amyamy=newAmy(); Alicealice=newAlice(); // Tom和Amy订阅Paper System.out.println("----Tom subscribe the paper----"); paper.registerObserver(tom); System.out.println("----Amy subscribe the paper----"); paper.registerObserver(amy); // 生产一份报纸,Tom和Amy收到报纸, Alice没订阅报纸,没收到通知 System.out.println("----make first paper: Tom and Amy will receive,but Alice not----"); paper.makeNewPaper("Fisrt Paper", "Hello World!"); // Alice觉得报纸不错,要订阅报纸 System.out.println("----Alice subscribe the paper----"); paper.registerObserver(alice); paper.registerObserver(alice); paper.registerObserver(alice); // 生产第二份报纸,Tom、Amy、Alice都收到报纸 System.out.println("----make secode paper: Tom、Amy and Alice will receive----"); paper.makeNewPaper("Second Paper", "Weclome Alice to join us!"); // Alice觉得报纸不适合自己,决定退订 System.out.println("----Alice unsubscribe the paper----"); paper.removeObserver(alice); // 生产第三份报纸,Tom和Amy能收到,而Alice已经收不到了 System.out.println("----make third paper: Tom and Amy will receive,but Alice not----"); paper.makeNewPaper("Third Paper", "Sorry for Alice leaving!"); } }
publicclassTestObserver { publicstaticvoidmain(String[] args) { // 初始化 Paperpaper=newPaper(); Tomtom=newTom(); Amyamy=newAmy(); Alicealice=newAlice(); // Tom和Amy订阅Paper System.out.println("----Tom subscribe the paper----"); paper.addObserver(tom); System.out.println("----Amy subscribe the paper----"); paper.addObserver(amy); // 生产一份报纸,Tom和Amy收到报纸, Alice没订阅报纸,没收到通知 System.out.println("----make first paper: Tom and Amy will receive,but Alice not----"); paper.makeNewPaper("Fisrt Paper", "Hello World!"); // Alice觉得报纸不错,要订阅报纸 System.out.println("----Alice subscribe the paper----"); paper.addObserver(alice); // 生产第二份报纸,Tom、Amy、Alice都收到报纸 System.out.println("----make secode paper: Tom、Amy and Alice will receive----"); paper.makeNewPaper("Second Paper", "Weclome Alice to join us!"); // Alice觉得报纸不适合自己,决定退订 System.out.println("----Alice unsubscribe the paper----"); paper.deleteObserver(alice); // 生产第三份保证,Tom和Amy能收到,而Alice已经收不到了 System.out.println("----make third paper: Tom and Amy will receive,but Alice not----"); paper.makeNewPaper("Third Paper", "Sorry for Alice leaving!"); } }