对象行为型——迭代器模式

模式定义
迭代器模式(Iterator Pattern) :提供一种方法来访问聚合对象,而不用暴露这个对象的内部表示,其别名为游标(Cursor)。迭代器模式是一种对象行为型模式。

模式结构
迭代器模式包含如下角色:

  • Iterator: 抽象迭代器
  • ConcreteIterator: 具体迭代器
  • Aggregate: 抽象聚合类
  • ConcreteAggregate: 具体聚合类
    迭代器模式结构

模式分析

  • 聚合是一个管理和组织数据对象的数据结构。
  • 聚合对象主要拥有两个职责:一是存储内部数据;二是遍历内部数据
  • 存储数据是聚合对象最基本的职责。
  • 遍历聚合对象中数据的行为提取出来,封装到一个迭代器中,通过专门的迭代器来遍历聚合对象的内部数据,这就是迭代器模式的本质。迭代器模式是“单一职责原则”的完美体现。

    自定义迭代器

  • MyIterator——抽象迭代器
  • MyCollection——抽象聚合类
  • NewCollection——具体聚合类
  • NewIterator——具体迭代器
  • Client

在迭代器模式中应用了工厂方法模式,聚合类充当工厂类,而迭代器充当产品类,由于定义了抽象层,系统的扩展性很好,在客户端可以针对抽象聚合类和抽象迭代器进行编程。

模式适用环境
在以下情况下可以使用迭代器模式:

  1. 访问一个聚合对象的内容而无须暴露它的内部表示。
  2. 需要为聚合对象提供多种遍历方式。
  3. 为遍历不同的聚合结构提供一个统一的接口。

实例:电视机遥控器就是一个迭代器的实例,通过它可以实现对电视机频道集合的遍历操作,本实例我们将模拟电视机遥控器的实现。
电视机遥控器

Television.java

public interface Television {
    public TVIterator createIterator();
}

TVIterator.java

public interface TVIterator {
    public boolean isFirst();
    public boolean isLast();
    public void next();
    public void previous();
    public void setChannel(int i);
    public Object currentChannel();
}

TCLTelevision.java

public class TCLTelevision implements Television {

    Object[] obj= {"cctv1","cctv2","cctv3","cctv4","cctv5","cctv6","cctv7"};
    @Override
    public TVIterator createIterator() {
        // TODO Auto-generated method stub
        return new TCLIterator();
    }
class TCLIterator implements TVIterator{

    private int currentIndex=0;
    @Override
    public boolean isFirst() {
        if(currentIndex==0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean isLast() {
        if(currentIndex==obj.length) {
            return true;
        }
        return false;
    }

    @Override
    public void next() {
        // TODO Auto-generated method stub
        if(currentIndex<obj.length) {
            currentIndex++;
        }else {
            currentIndex=0;
        }
    }

    @Override
    public void previous() {
        // TODO Auto-generated method stub
        if(currentIndex<obj.length) {
            currentIndex--;
        }else {
            currentIndex=obj.length-1;
        }
    }

    @Override
    public void setChannel(int i) {
        // TODO Auto-generated method stub
        currentIndex=i;
    }

    @Override
    public Object currentChannel() {
        // TODO Auto-generated method stub
        return obj[currentIndex];
    }

 }
}

Client.java

public class Client {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         Television tv;
         tv=new TCLTelevision();
         TVIterator it=tv.createIterator();


         while(!it.isLast()) {
             System.out.println(it.currentChannel().toString());
             it.next();
         }
         System.out.println("^^^^^^^^^^^^^^^^^^^^^^");
         it.setChannel(4);
         System.out.println(it.currentChannel().toString());
         System.out.println("^^^^^^^^^^^^^^^^^^^^^^");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39026548/article/details/80226661