17) Iterator pattern

发布时间 2023-06-09 13:41:41作者: zno2

类别:

 behavioral pattern

问题:

 Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

方案:

 

示例:

 

import java.util.ArrayList;
import java.util.List;

public class IteratorPatternDemo {
    public static void main(String[] args) {
        ChannelCollection channelList = new ChannelCollectionImpl();
        channelList.add(new Channel(98.5, ChannelTypeEnum.ENGLISH));
        channelList.add(new Channel(99.5, ChannelTypeEnum.HINDI));
        channelList.add(new Channel(100.5, ChannelTypeEnum.FRENCH));
        channelList.add(new Channel(101.5, ChannelTypeEnum.ENGLISH));
        channelList.add(new Channel(102.5, ChannelTypeEnum.HINDI));
        channelList.add(new Channel(103.5, ChannelTypeEnum.FRENCH));
        channelList.add(new Channel(104.5, ChannelTypeEnum.ENGLISH));
        channelList.add(new Channel(105.5, ChannelTypeEnum.HINDI));
        channelList.add(new Channel(106.5, ChannelTypeEnum.FRENCH));

        System.out.println("------ ALL --------");
        ChannelIterator iterator = channelList.iterator(ChannelTypeEnum.ALL);
        while (iterator.hasNext()) {
            Channel c = iterator.next();
            System.out.println(c);
        }

        System.out.println("------ ENGLISH --------");
        ChannelIterator englishIterator = channelList.iterator(ChannelTypeEnum.ENGLISH);
        while (englishIterator.hasNext()) {
            Channel c = englishIterator.next();
            System.out.println(c);
        }
    }

}

enum ChannelTypeEnum {

    ENGLISH, HINDI, FRENCH, ALL;
}

class Channel {

    private double frequency;
    private ChannelTypeEnum type;

    public Channel(double freq, ChannelTypeEnum type) {
        this.frequency = freq;
        this.type = type;
    }

    public double getFrequency() {
        return frequency;
    }

    public ChannelTypeEnum getTYPE() {
        return type;
    }

    @Override
    public String toString() {
        return "Frequency=" + this.frequency + ", Type=" + this.type;
    }

}

interface ChannelCollection {

    public void add(Channel c);

    public void remove(Channel c);

    public ChannelIterator iterator(ChannelTypeEnum type);

}

interface ChannelIterator {

    public boolean hasNext();

    public Channel next();
}

class ChannelCollectionImpl implements ChannelCollection {

    private List<Channel> channelList;

    public ChannelCollectionImpl() {
        channelList = new ArrayList<Channel>();
    }

    @Override
    public void add(Channel c) {
        this.channelList.add(c);
    }

    @Override
    public void remove(Channel c) {
        this.channelList.remove(c);
    }

    @Override
    public ChannelIterator iterator(ChannelTypeEnum type) {
        return new ChannelIteratorImpl(type, this.channelList);
    }

    private class ChannelIteratorImpl implements ChannelIterator {

        private ChannelTypeEnum type;
        private List<Channel> channelList;
        private int position;

        public ChannelIteratorImpl(ChannelTypeEnum ty, List<Channel> channelList) {
            this.type = ty;
            this.channelList = channelList;
        }

        @Override
        public boolean hasNext() {
            while (position < channelList.size()) {
                Channel c = channelList.get(position);
                if (c.getTYPE().equals(type) || type.equals(ChannelTypeEnum.ALL)) {
                    return true;
                } else
                    position++;
            }
            return false;
        }

        @Override
        public Channel next() {
            Channel c = channelList.get(position++);
            return c;
        }

    }
}

 

------ ALL --------
Frequency=98.5, Type=ENGLISH
Frequency=99.5, Type=HINDI
Frequency=100.5, Type=FRENCH
Frequency=101.5, Type=ENGLISH
Frequency=102.5, Type=HINDI
Frequency=103.5, Type=FRENCH
Frequency=104.5, Type=ENGLISH
Frequency=105.5, Type=HINDI
Frequency=106.5, Type=FRENCH
------ ENGLISH --------
Frequency=98.5, Type=ENGLISH
Frequency=101.5, Type=ENGLISH
Frequency=104.5, Type=ENGLISH

 

应用:

 

不足:(

 

优化:)