标签:style blog java color 使用 文件
package com.observer; public interface Subject { void addObserver(Observer o); void deleteObserver(Observer o); void notifyObservers(); }
2.具体主题
package com.observer; import java.util.ArrayList; public class SeekJobCenter implements Subject { String mess; boolean changed; ArrayList<Observer> personList; public SeekJobCenter() { personList = new ArrayList<Observer>(); mess = ""; changed = false; } //添加求职者 public void addObserver(Observer o) { if(!(personList.contains(o))){ personList.add(o); } } //删除求职者 public void deleteObserver(Observer o) { if(personList.contains(o)){ personList.remove(o); } } //通知所有求职者 public void notifyObservers() { if(changed){ for (int i = 0; i < personList.size(); i++) { Observer o = personList.get(i); o.hearTelephone(mess); } changed = false; } } //刷新信息 public void giveNewMess(String str){ if(str.equals(mess)){ changed = false; }else{ mess = str; changed = true; } } }
3、观察者
package com.observer; public interface Observer { public void hearTelephone(String mess); }
package com.observer; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; public class UniversityStudent implements Observer{ Subject subject; File myFile; public UniversityStudent(Subject subject,String fileName) { this.subject = subject; subject.addObserver(this); myFile = new File(fileName); } public void hearTelephone(String mess) { try { RandomAccessFile out = new RandomAccessFile(myFile,"rw"); out.seek(out.length()); byte[] b = mess.getBytes(); out.write(b); System.out.println("我是会员一"); System.out.println("我向文件"+myFile.getName()+"写入如下内容:"); System.out.println(mess); } catch (IOException e) { e.printStackTrace(); } } }
UniversityStudent2.java
package com.observer; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; public class UniversityStudent2 implements Observer { Subject subject; File myFile; public UniversityStudent2(Subject subject,String fileName) { this.subject = subject; subject.addObserver(this); myFile = new File(fileName); } public void hearTelephone(String mess) { try { boolean boo = mess.contains("促销员")||mess.contains("发单员"); if(boo){ RandomAccessFile out = new RandomAccessFile(myFile,"rw"); out.seek(out.length()); byte[] b = mess.getBytes(); out.write(b); System.out.println("我是会员二"); System.out.println("我向文件"+myFile.getName()+"写入如下内容:"); System.out.println(mess); } } catch (IOException e) { e.printStackTrace(); } } }
下面写一个测试程序:
package com.observer; public class Application { /** * @param args */ public static void main(String[] args) { SeekJobCenter center = new SeekJobCenter(); UniversityStudent zhanglin = new UniversityStudent(center,"zhanglin.txt"); UniversityStudent2 wangHao = new UniversityStudent2(center,"wanghao.txt"); center.giveNewMess("XX公司需要10个促销员。"); center.notifyObservers(); center.giveNewMess("XX公司需要8个发单员。"); center.notifyObservers(); center.giveNewMess("XX公司需要9个临时工。"); center.notifyObservers(); center.giveNewMess("XX公司需要9个临时工。"); center.notifyObservers(); } }
运行结果如下
我是会员一
我向文件zhanglin.txt写入如下内容:
XX公司需要10个促销员。
我是会员二
我向文件wanghao.txt写入如下内容:
XX公司需要10个促销员。
我是会员一
我向文件zhanglin.txt写入如下内容:
XX公司需要8个发单员。
我是会员二
我向文件wanghao.txt写入如下内容:
XX公司需要8个发单员。
我是会员一
我向文件zhanglin.txt写入如下内容:
XX公司需要9个临时工。
package com.observer1; public interface Subject { void addObserver(Observer o); void deleteObserver(Observer o); void notifyObservers(); }
2.具体主题
package com.observer1; import java.util.ArrayList; public class ShopSubject implements Subject { String goodsName; double oldPrice,newPrice; ArrayList<Observer> customerList; public ShopSubject() { customerList = new ArrayList<Observer>(); } public void addObserver(Observer o) { if(!(customerList.contains(o))){ customerList.add(o); } } public void deleteObserver(Observer o) { if(customerList.contains(o)){ customerList.remove(o); } } public void notifyObservers() { for (int i = 0; i < customerList.size(); i++) { Observer o = customerList.get(i); o.update(); } } //设置打折商品 public void setDiscountGoods(String name,double oldP,double newP){ goodsName = name; oldPrice = oldP; newPrice = newP; //设置完通知顾客 notifyObservers(); } public String getGoodsName() { return goodsName; } public double getOldPrice() { return oldPrice; } public double getNewPrice() { return newPrice; } }
package com.observer1; public interface Observer { public void update(); }
4.具体观察者
package com.observer1; public class CustomerOne implements Observer { Subject subject; String goodsName,personName; public CustomerOne(Subject subject,String personName) { this.personName = personName; this.subject = subject; subject.addObserver(this); } public void update() { if(subject instanceof ShopSubject){ goodsName = ((ShopSubject)subject).getGoodsName(); System.out.println(personName+"只对打折商品的名字感兴趣:"); System.out.println("打折商品的名字是:"+goodsName); } } }
CustomerTwo.java’
package com.observer1; public class CustomerTwo implements Observer { Subject subject; String personName; double oldPrice,newPrice; public CustomerTwo(Subject subject,String personName) { this.personName = personName; this.subject = subject; subject.addObserver(this); } public void update() { if(subject instanceof ShopSubject){ oldPrice = ((ShopSubject)subject).getOldPrice(); newPrice = ((ShopSubject)subject).getNewPrice(); System.out.println(personName+"只对打折商品的原价和折后价感兴趣:"); System.out.println("打折商品的原价是:"+oldPrice); System.out.println("打折商品的折后价是:"+newPrice); } } }
下面写一个测试例子:
package com.observer1; public class Application { public static void main(String[] args) { ShopSubject shop = new ShopSubject(); CustomerOne boy = new CustomerOne(shop,"小明"); CustomerTwo girl = new CustomerTwo(shop,"小红"); shop.setDiscountGoods("Photo数码相机", 2345.6, 2020.0); shop.setDiscountGoods("三星手机", 2999.0, 2499.0); } }
运行结果为:
小明只对打折商品的名字感兴趣:
打折商品的名字是:Photo数码相机
小红只对打折商品的原价和折后价感兴趣:
打折商品的原价是:2345.6
打折商品的折后价是:2020.0
小明只对打折商品的名字感兴趣:
打折商品的名字是:三星手机
小红只对打折商品的原价和折后价感兴趣:
打折商品的原价是:2999.0
打折商品的折后价是:2499.0
package com.observer2; public interface Subject { void addObserver(Observer o); void deleteObserver(Observer o); void notifyObservers(); }
2.观察者
package com.observer2; public interface Observer { public void update(Subject subject); }
3.具体主题
package com.observer2; import java.util.ArrayList; public class TravelAgency implements Subject{ String tourStartTime; String tourMess; ArrayList<Observer> personList; public TravelAgency() { personList = new ArrayList<Observer>(); } public void addObserver(Observer o) { if(o == null){ return; }else{ if(!(personList.contains(o))){ personList.add(o); } } } public void deleteObserver(Observer o) { if(personList.contains(o)){ personList.remove(o); } } public void notifyObservers() { for (int i = 0; i < personList.size(); i++) { Observer o = personList.get(i); o.update(this); } } public void giveMess(String time,String mess){ tourStartTime = time; tourMess = mess; notifyObservers(); } public String getTourStartTime() { return tourStartTime; } public String getTourMess() { return tourMess; } }
WeaherStation.java
package com.observer2; import java.util.ArrayList; public class WeaherStation implements Subject{ String forecastTime,forcastMess; int maxPemperature,minTemperature; ArrayList<Observer> personList; public WeaherStation() { personList = new ArrayList<Observer>(); } public void addObserver(Observer o) { if(o == null){ return; }else{ if(!(personList.contains(o))){ personList.add(o); } } } public void deleteObserver(Observer o) { if(personList.contains(o)){ personList.remove(o); } } public void notifyObservers() { for (int i = 0; i < personList.size(); i++) { Observer o = personList.get(i); o.update(this); } } public void doForeCast(String t,String mess,int max,int min){ forecastTime = t; forcastMess = mess; minTemperature = min; maxPemperature = max; notifyObservers(); } public String getForecastTime() { return forecastTime; } public String getForcastMess() { return forcastMess; } public int getMaxPemperature() { return maxPemperature; } public int getMinTemperature() { return minTemperature; } }
具体观察者
package com.observer2; public class Person implements Observer { Subject subjectOne,subjectTwo; //可依赖的主题 String forecastTime,forecastMess; String tourStartTime,tourMess; int maxTemperature,minTemperature; public Person(Subject one,Subject two) { this.subjectOne = one; this.subjectTwo = two; subjectOne.addObserver(this); subjectTwo.addObserver(this); } public void update(Subject subject) { if(subject instanceof WeaherStation){ WeaherStation ws = (WeaherStation)subject; forecastTime = ws.getForecastTime(); forecastMess = ws.getForcastMess(); maxTemperature = ws.getMaxPemperature(); minTemperature = ws.getMinTemperature(); System.out.print("预报日期:"+forecastTime+","); System.out.print("天气状况:"+forecastMess+","); System.out.print("最高温度:"+maxTemperature+","); System.out.println("最低温度:"+minTemperature+"."); }else if(subject instanceof TravelAgency){ TravelAgency ta = (TravelAgency)subject; tourStartTime = ta.getTourStartTime(); tourMess = ta.getTourMess(); System.out.print("旅游开始日期:"+tourStartTime+","); System.out.println("旅游信息:"+tourMess+"."); } } }
下面写一个测试程序
package com.observer2; public class Application { public static void main(String[] args) { WeaherStation weaherStation = new WeaherStation();//具体主题 TravelAgency travelAgency = new TravelAgency(); //具体主题 Person boy = new Person(weaherStation,travelAgency); weaherStation.doForeCast("10日", "阴有小雨", 28, 20); travelAgency.giveMess("10日", "黄山2日游"); weaherStation.doForeCast("11日", "晴转多云", 30, 21); travelAgency.giveMess("11日", "丽江1日游"); } }
运行结果如下
预报日期:10日,天气状况:阴有小雨,最高温度:28,最低温度:20.
旅游开始日期:10日,旅游信息:黄山2日游.
预报日期:11日,天气状况:晴转多云,最高温度:30,最低温度:21.
旅游开始日期:11日,旅游信息:丽江1日游.
标签:style blog java color 使用 文件
原文地址:http://www.cnblogs.com/evolcq/p/3819273.html