Domo is Fugito.
This time, Hiroshi Yuki "Introduction to Design Patterns Learned in Java Language" Design pattern while referring to (SB Creative) We will implement the "Iterator pattern" which is one of the I will.
The Iterator pattern is a collection of things Sometimes, instruct it in order and scan the whole It is for performing the processing to be done. (P.2)
For example, "The identity of the things in the bag one by one. I think it's a program called "search".
In this book, as a sample program, "Enter in the bookshelf" Look up the books and display the names of the books in order. " Is introduced. This time I added an arrangement there "** Display songs in the song list one by one **" I thought I would write the program in the Iterator pattern I will.
Aggregate interface counts Represents an "aggregate" of things to do. (P.3)
Here, "Song Aggregate", which stands for "collection of songs" Create an interface called.
public interface SongAggregate {
public abstract SongIterator songIterator();
}
This interface counts aggregates Declare only the method "songIterator" for.
The Iterator interface counts elements Acts like a loop variable It is a thing.
Here, create "Song Iterator".
public interface SongIterator {
public abstract boolean hasNext();
public abstract Object next();
}
Does the hasNext method have the following elements in the "aggregate"? It is for checking. Also, the next method If you have the following element, you will get that element.
This is a class for expressing each "song".
public class Song {
private String songName;
public Song(String s) {
this.songName = s;
}
public String getSongName() {
return songName;
}
}
This class expresses "a collection of songs". Implement the SongAggregate interface and use abstract methods Override the songIterator method that was there.
public class SongList implements SongAggregate {
private Song[] songs;
private int last = 0;
public SongList(int max) {
this.songs = new Song[max];
}
public Song getSongAt(int id) {
return songs[id];
}
public void appendSong(Song s) {
this.songs[last] = s;
last++;
}
public int getLength() {
return last;
}
@Override
public SongIterator songIterator() {
return new SongListIterator(this);
}
}
Here the constructor maximizes the song Define SongList instance that can store up to doing. The getSongAt method returns the idth song. The appendSong method is at the end of the array songs [] Stores a new song s. getLength returns the number of songs in the SongList The method. And in the overridden songIterator method, Of the class called SongListIterator that we will create Create an instance and return it as a SongIterator type.
Actually scan the SongList class It is a class. Real SongIterator interface Pretending to be over the hasNext method and next method Ride
public class SongListIterator implements SongIterator {
private SongList songList;
private int id;
public SongListIterator(SongList sl) {
this.songList = sl;
this.id = 0;
}
@Override
public boolean hasNext() {
if(id < songList.getLength()) {
return true;
}else {
return false;
}
}
@Override
public Object next() {
Song song = songList.getSongAt(id);
id++;
return song;
}
}
Now you are ready to scan the list. from here Create a Main class to create an actual list and scan I will try.
public class Main {
public static void main(String[] args) {
//新しくSongListを作成
SongList songList = new SongList(5);
//リストに曲を追加
songList.appendSong(new Song("So What"));
songList.appendSong(new Song("Freddie Freeloader"));
songList.appendSong(new Song("Blue In Green"));
songList.appendSong(new Song("All Blues"));
songList.appendSong(new Song("Flamenco Sketches"));
//SongIteratorのインスタンスを生成
SongIterator song_it = songList.songIterator();
//曲をひとつずつ調べる
while(song_it.hasNext()) {
Song song = (Song)song_it.next();
System.out.println(song.getSongName());
}
}
}
The execution result is as follows.
So What
Freddie Freeloader
Blue In Green
All Blues
Flamenco Sketches
It seems that it was executed properly. Good grief.
After all, the point of the Iterator pattern is "** Even if I rewrite the SongList, the songIterator method still works. If it exists and returns the correct SongIterator, the Main message Sod's while loop works without any changes ** " It is at the point. In short, a design that can withstand the expansion of functions It means that it has become. It ’s just “wisdom in the field” I feel like that.
From now on, 23 GoF design patterns will be used like this. I wish I could put it together.
so, that's it for today. It was Puyi.
P.S. "Seniors", opinions on this article, Please feel free to comment if you have any supplements m (_ _) m
Recommended Posts