java设计模式
2014-04-13 22:22
429 查看
15.观察者模式:购物网站上用户添加到货通知后,商品有货后,就会主动通知订阅到货通知的用户
对用户操作接口:
实现类:
如果要增加其他通知类型,可以在Observer接口新增方法,例如消息推送,
16.迭代器模式:先顺序访问对象,再对对象中元素遍历访问,例如Iterator ite=Collection.iterator ite.hasNext();
Collection中可以获取Iterator
测试类:
22
public interface Observer { public void update(); }
public class Observer1 implements Observer { public void update() { System.out.println("Observer1 has received notify"); } }
public class Observer2 implements Observer { public void update() { System.out.println("Observer2 has received notify"); } }
对用户操作接口:
public interface Subject { public void add(Observer observer); public void del(Observer observer); public void notifyAllObservers(); public void operation(); }
public class AbstractObserver implements Subject { private Vector<Observer> vector = new Vector<Observer>(); public void add(Observer observer) { vector.add(observer); } public void del(Observer observer) { vector.remove(observer); } public void notifyAllObservers() { Iterator<Observer> iterator = (Iterator<Observer>) vector.iterator(); while (iterator.hasNext()) { Observer observer = iterator.next(); observer.update(); } } public void operation() { //To change body of implemented methods use File | Settings | File Templates. } }
实现类:
public class MySubject extends AbstractObserver { public void operation(){ notifyAllObservers(); } }
public class Test { public static void main(String[] args) { Subject subject = new MySubject(); subject.add(new Observer1()); Observer2 observer2=new Observer2(); subject.add(observer2); subject.operation(); System.out.println("============="); subject.del(observer2); subject.operation(); } }
如果要增加其他通知类型,可以在Observer接口新增方法,例如消息推送,
16.迭代器模式:先顺序访问对象,再对对象中元素遍历访问,例如Iterator ite=Collection.iterator ite.hasNext();
public interface Iterator { public Object previous(); public Object next(); public boolean hasNext(); public Object first(); }
public interface Collection { public Iterator iterator(); public Object get(int i); public int size(); }
Collection中可以获取Iterator
public class MyCollection implements Collection { String[] strings = {"a","b","c"}; public Iterator iterator() { return new MyIterator(this); //To change body of implemented methods use File | Settings | File Templates. } public Object get(int i) { return strings[i]; //To change body of implemented methods use File | Settings | File Templates. } public int size() { return strings.length; //To change body of implemented methods use File | Settings | File Templates. } }
public class MyIterator implements Iterator { private int pos = -1; private Collection collection; public MyIterator(Collection collection) { this.collection = collection; } public Object previous() { if (pos > 0) { pos--; } return collection.get(pos); //To change body of implemented methods use File | Settings | File Templates. } public Object next() { if (pos < collection.size() - 1) { pos++; } return collection.get(pos); //To change body of implemented methods use File | Settings | File Templates. } public boolean hasNext() { if (pos < collection.size() - 1) { return true; } return false; } public Object first() { if (collection.size() > 0) { return collection.get(0); } return null; } }
测试类:
public class Test { public static void main(String[] args) { Collection collection = new MyCollection(); Iterator iterator = collection.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } }
22
相关文章推荐
- Java设计模式_(结构型)_外观模式
- 关于Java 23种设计模式的有趣见解
- 黑马程序员--Java学习11--设计模式
- Java 设计模式 之 状态模式(State)
- Java动态代理(设计模式)代码详解
- java设计模式--事件监听器模式和观察者模式
- Java设计模式之单例模式
- JAVA设计模式之外观模式
- java设计模式之适配器设计模式
- java设计模式-桥接模式
- java设计模式之单例模式
- Java设计模式之五种单例模式
- java 设计模式之我见
- Java开发中的23种设计模式之四:建造者模式(Builder)
- 四、Java设计模式——模板模式
- Java设计模式之构造者模式,你真正了解过吗
- 设计模式--创建模式--工厂方法模式--java
- java设计模式----->单例设计模式
- JAVA设计模式之访问者模式详解
- java设计模式之命令模式