迭代器模式

来源:互联网 发布:如何获得股票数据 编辑:程序博客网 时间:2024/05/30 05:29

Iterator pattern in one of the behavioral pattern and it’s used to provide a standard way to traverse through a group of Objects. Iterator pattern is widely used inJava Collection Framework where Iterator interface provides methods for traversing through a collection. According to GoF, iterator design pattern intent is:

Provides a way to access the elements of an aggregate object without exposing its underlying represenation.

Iterator pattern is not only about traversing through a collection, we can provide different kind of iterators based on our requirements. Iterator pattern hides the actual implementation of traversal through the collection and client programs just use iterator methods.

Let’s understand this pattern with a simple example. Suppose we have a list of Radio channels and the client program want to traverse through them one by one or based on the type of channel, for example some client programs are only interested in English channels and want to process only them, they don’t want to process other types of channels.

So we can provide a collection of channels to the client and let them write the logic to traverse through the channels and decide whether to process them. But this solution has lots of issues such as client has to come up with the logic for traversal. We can’t make sure that client logic is correct and if the number of client grows then it will become very hard to maintain.

Here we can use Iterator pattern and provide iteration based on type of channel. We should make sure that client program can access the list of channels only through the iterator.

The first part of implementation is to define the contract for our collection and iterator interfaces.

ChannelTypeEnum.java
1
2
3
4
5
6
package com.journaldev.design.iterator;
 
public enum ChannelTypeEnum {
 
    ENGLISH, HINDI, FRENCH, ALL;
}

ChannelTypeEnum is java enum that defines all the different types of channels.

Channel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.journaldev.design.iterator;
 
public class Channel {
 
    privatedoublefrequency;
    privateChannelTypeEnum TYPE;
     
    publicChannel(doublefreq, ChannelTypeEnum type){
        this.frequency=freq;
        this.TYPE=type;
    }
 
    publicdoublegetFrequency() {
        returnfrequency;
    }
 
    publicChannelTypeEnum getTYPE() {
        returnTYPE;
    }
     
    @Override
    publicString toString(){
        return"Frequency="+this.frequency+", Type="+this.TYPE;
    }
     
}

Channel is a simple POJO class that has attributes frequency and channel type.

ChannelCollection.java
1
2
3
4
5
6
7
8
9
10
11
12
package com.journaldev.design.iterator;
 
 
public interface ChannelCollection {
 
    publicvoidaddChannel(Channel c);
     
    publicvoidremoveChannel(Channel c);
     
    publicChannelIterator iterator(ChannelTypeEnum type);
     
}

ChannelCollection interface defines the contract for our collection class implementation. Notice that there are methods to add and remove a channel but there is no method that returns the list of channels and it has a method that returns the iterator for traversal. ChannelIterator interface defines following methods;

ChannelIterator.java
1
2
3
4
5
6
7
8
package com.journaldev.design.iterator;
 
public interface ChannelIterator {
 
    publicbooleanhasNext();
     
    publicChannel next();
}

Now our base interface and core classes are ready, let’s proceed with the implementation of collection class and iterator.

ChannelCollectionImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.journaldev.design.iterator;
 
import java.util.ArrayList;
import java.util.List;
 
public class ChannelCollectionImpl implementsChannelCollection {
 
    privateList<Channel> channelsList;
 
    publicChannelCollectionImpl() {
        channelsList =newArrayList<>();
    }
 
    publicvoidaddChannel(Channel c) {
        this.channelsList.add(c);
    }
 
    publicvoidremoveChannel(Channel c) {
        this.channelsList.remove(c);
    }
 
    @Override
    publicChannelIterator iterator(ChannelTypeEnum type) {
        returnnewChannelIteratorImpl(type,this.channelsList);
    }
 
    privateclassChannelIteratorImplimplementsChannelIterator {
 
        privateChannelTypeEnum type;
        privateList<Channel> channels;
        privateintposition;
 
        publicChannelIteratorImpl(ChannelTypeEnum ty,
                List<Channel> channelsList) {
            this.type = ty;
            this.channels = channelsList;
        }
 
        @Override
        publicbooleanhasNext() {
            while(position < channels.size()) {
                Channel c = channels.get(position);
                if(c.getTYPE().equals(type) || type.equals(ChannelTypeEnum.ALL)) {
                    returntrue;
                }else
                    position++;
            }
            returnfalse;
        }
 
        @Override
        publicChannel next() {
            Channel c = channels.get(position);
            position++;
            returnc;
        }
 
    }
}

Notice the inner class implementation of iterator interface so that the implementation can’t be used by any other collection. Same approach is followed by collection classes also and all of them have inner class implementation of Iterator interface.

Let’s write a simple test class to use our collection and iterator to traverse through the collection of channels based on type.

IteratorPatternTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.journaldev.design.iterator;
 
 
public class IteratorPatternTest {
 
    publicstaticvoid main(String[] args) {
        ChannelCollection channels = populateChannels();
        ChannelIterator baseIterator = channels.iterator(ChannelTypeEnum.ALL);
        while(baseIterator.hasNext()) {
            Channel c = baseIterator.next();
            System.out.println(c.toString());
        }
        System.out.println("******");
        // Channel Type Iterator
        ChannelIterator englishIterator = channels.iterator(ChannelTypeEnum.ENGLISH);
        while(englishIterator.hasNext()) {
            Channel c = englishIterator.next();
            System.out.println(c.toString());
        }
    }
 
    privatestaticChannelCollection populateChannels() {
        ChannelCollection channels =newChannelCollectionImpl();
        channels.addChannel(newChannel(98.5, ChannelTypeEnum.ENGLISH));
        channels.addChannel(newChannel(99.5, ChannelTypeEnum.HINDI));
        channels.addChannel(newChannel(100.5, ChannelTypeEnum.FRENCH));
        channels.addChannel(newChannel(101.5, ChannelTypeEnum.ENGLISH));
        channels.addChannel(newChannel(102.5, ChannelTypeEnum.HINDI));
        channels.addChannel(newChannel(103.5, ChannelTypeEnum.FRENCH));
        channels.addChannel(newChannel(104.5, ChannelTypeEnum.ENGLISH));
        channels.addChannel(newChannel(105.5, ChannelTypeEnum.HINDI));
        channels.addChannel(newChannel(106.5, ChannelTypeEnum.FRENCH));
        returnchannels;
    }
 
}

When I run above program, it produces following output;

1
2
3
4
5
6
7
8
9
10
11
12
13
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
******
Frequency=98.5, Type=ENGLISH
Frequency=101.5, Type=ENGLISH
Frequency=104.5, Type=ENGLISH

Important Points

  • Iterator pattern is useful when you want to provide a standard way to iterate over a collection and hide the implementation logic from client program.
  • The logic for iteration is embedded in the collection itself and it helps client program to iterate over them easily.

Iterator Pattern in JDK

We all know that Collection framework Iterator is the best example of iterator pattern implementation but do you know thatjava.util.Scanner class also Implements Iterator interface. Read this post to learn aboutJava Scanner Class.

That’s all for iterator pattern, I hope it’s helpful and easy to understand.

0 0
原创粉丝点击