标签:
参考的优秀文章:
假设,Pay是指收入,Tax是指个人所得税,SocialSecurity是社保,而后两者是以Pay为基数计算所得的。
所以,Tax、SocialSecurity依赖于Pay,Pay一旦变动,其他两者也随之变动。
没有使用观察者模式的代码,可以这么写:
package No002没有观察者模式的时候; public class Pay { private Integer userId; private Integer pay; public Pay(Integer userId, Integer pay) { super(); this.userId = userId; this.pay = pay; } public void update(Integer pay) { this.pay = pay; System.out.println("Update the user " + this.userId + "‘s pay to " + this.pay); } public Integer getUserId() { return userId; } public Integer getPay() { return pay; } }
package No002没有观察者模式的时候; public class Tax { public void update(Pay pay) { System.out.println("Update the user " + pay.getUserId() + "‘s tax to " + pay.getPay()); } }
package No002没有观察者模式的时候; import No002没有观察者模式的时候.Pay; public class SocialSecurity { public void update(Pay pay) { System.out.println("Update the user " + pay.getUserId() + "‘s social securoty to " + pay.getPay()); } }
package No002没有观察者模式的时候; public class PayService { public void payChange(Pay pay) { Tax tax = new Tax(); SocialSecurity ss = new SocialSecurity(); pay.update(pay.getPay()); tax.update(pay); ss.update(pay); } }
package No002没有观察者模式的时候; public class Client { public static void main(String[] args) { Pay pay = new Pay(001, 12000); PayService ps = new PayService(); ps.payChange(pay); } }
而使用观察者模式,代码可以这么写。(这里并没有从零开始写观察者模式,而是使用java.util下观察者模式的基础包)
java.util.Observer是观察者的接口,java.util.Observable是被观察者(发生变动的根源)的父类。
package No002使用Java的观察者类; public class Pay { private Integer userId; private Integer pay; public Pay(Integer userId, Integer pay) { super(); this.userId = userId; this.pay = pay; } public void update(Integer pay) { this.pay = pay; System.out.println("Update the user " + this.userId + "‘s pay to " + this.pay); } public Integer getUserId() { return userId; } public Integer getPay() { return pay; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Pay [userId=").append(userId).append(", pay=") .append(pay).append("]"); return builder.toString(); } }
package No002使用Java的观察者类.observer; import java.util.Observable; import java.util.Observer; import No002使用Java的观察者类.Pay; public class Tax implements Observer { public void update(Observable o, Object obj) { if (obj == null || !(obj instanceof Pay)) { return; } Pay pay = (Pay)obj; System.out.println("Update the user " + pay.getUserId() + "‘s tax to " + pay.getPay()); } }
package No002使用Java的观察者类.observer; import java.util.Observable; import java.util.Observer; import No002没有观察者模式的时候.Pay; public class SocialSecurity implements Observer { public void update(Observable o, Object obj) { if (obj == null || !(obj instanceof Pay)) { return; } Pay pay = (Pay)obj; System.out.println("Update the user " + pay.getUserId() + "‘s social securoty to " + pay.getPay()); } }
package No002使用Java的观察者类; import java.util.Observable; public class PayService extends Observable { public void payChange(Pay pay) { pay.update(pay.getPay()); this.setChanged(); this.notifyObservers(pay); } }
package No002使用Java的观察者类; import No002使用Java的观察者类.observer.SocialSecurity; import No002使用Java的观察者类.observer.Tax; public class Client { public static void main(String[] args) { PayService p = new PayService(); p.addObserver(new Tax()); p.addObserver(new SocialSecurity()); System.out.println("countObservers -> " + p.countObservers()); Pay pay = new Pay(001, 12000); p.payChange(pay); } }
标签:
原文地址:http://www.cnblogs.com/nick-huang/p/5054757.html