In: Computer Science
Use the iterator pattern to design and code a software (program) for some meaningful application (different from the book).Turn in UML diagram(s) and code with screenshots showing the code is working (all in one word file).
Please write an example of this pattern in Java. Thank you!
SAMPLE APPLICATION EXAMPLE
Let's assume we have an online streaming application like Netflix having list of multilinguistic movies and the client program want to traverse through them 1 by 1 or based on the type of movie language. For example some client programs are only interested in Spanish movies and want to process(watch) only them, they don’t want to process(watch) movies in any other language.
We can provide a collection of languages to the client and let them write the logic to traverse through the language and decide whether to process them or not. But this solution has multiple issues:
1) Client has to come up with the logic for traversal.
2) Also, we can’t make sure that client logic is correct.
3) Furthermore 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 movie language. We should make sure that client program can access the list of languages only through the iterator.
The first part of implementation defines the contract for our collection and iterator interfaces.
LanguageTypeEnum.java (This is java enum that defines all the different types of languages.)
package com.netflix.design.iterator;
public enum LanguageTypeEnum { ENGLISH, HINDI, FRENCH, ALL; }
Language.java (This is a simple POJO class that has attributes genre and language type.)
package com.netflix.design.iterator;
public class Language {
private string genre;
private LanguageTypeEnum TYPE;
public Language(string genre, LanguageTypeEnum
type){
this.genre=gen
this.TYPE=type;
}
public string getGenre() {
return genre;
}
public LanguageTypeEnum getTYPE() {
return TYPE;
}
@Override
public String toString(){
return "Genre="+this.genre+",
Type="+this.TYPE;
}
}
LanguageCollection.java
(This
interface defines the contract
for our collection class implementation. Note: there are methods to
add and remove a language but there is no method that returns the
list of language. ChannelCollection has a method that returns the
iterator for traversal.)
package com.netflix.design.iterator;
public interface LanguageCollection
{ public void addLanguage(Language l);
public void removeLanguage(Language l);
public LanguageIterator iterator(LanguageTypeEnum type); }
LanguageIterator.java
(
LanguageIterator interface defines
following methods)
package com.netflix.design.iterator;
public interface LanguageIterator
{ public boolean hasNext();
public Language next(); }
LanguageCollectionImpl.java
(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.)
package com.netflix.design.iterator;
import java.util.ArrayList;
import java.util.List;
public class LanguageCollectionImpl implements
LanguageCollection {
private List<Language>
languagesList;
public LanguageCollectionImpl() {
languagesList = new
ArrayList<>();
}
public void addLanguage(Language l) {
this.languagesList.add(l);
}
public void removeLanguage(Language l)
{
this.languagesList.remove(l);
}
@Override
public LanguageIterator iterator(LanguageTypeEnum
type) {
return new
LanguageIteratorImpl(type, this.languagesList);
}
private class LanguageIteratorImpl implements
LanguageIterator {
private LanguageTypeEnum
type;
private List<Language>
languages;
private int position;
public
LanguageIteratorImpl(LanguageTypeEnum ty,
List<Channel> languagesList) {
this.type =
ty;
this.languages =
languagesList;
}
@Override
public boolean hasNext() {
while (position
< languages.size()) {
Language l = languages.get(position);
if (l.getTYPE().equals(type) ||
type.equals(LanguageTypeEnum.ALL)) {
return true;
} else
position++;
}
return
false;
}
@Override
public Language next() {
Language l =
languages.get(position);
position++;
return l;
}
}
}
IteratorPatternTest.java
(
a simple iterator pattern test program
to use our collection and iterator to traverse through the
collection of languages)
package com.netflix.design.iterator;
public class IteratorPatternTest {
public static void main(String[] args)
{
LanguageCollection languages =
populateLanguages();
LanguageIterator baseIterator =
languages.iterator(LanguageTypeEnum.ALL);
while (baseIterator.hasNext())
{
Language l =
baseIterator.next();
System.out.println(l.toString());
}
System.out.println("******");
// Language Type Iterator
LanguageIterator englishIterator
=languages.iterator(LanguageTypeEnum.ENGLISH);
while (englishIterator.hasNext())
{
Language l =
englishIterator.next();
System.out.println(l.toString());
}
}
private static LanguageCollection
populateLanguages() {
LanguageCollection languages = new
LanguageCollectionImpl();
languages.addLanguage(new
Language("Action", LanguageTypeEnum.ENGLISH));
languages.addLanguage(new
Language("Drama", LanguageTypeEnum.HINDI));
languages.addLanguage(new
Language("Action", LanguageTypeEnum.FRENCH));
languages.addLanguage(new
Language("Drama", LanguageTypeEnum.ENGLISH));
languages.addLanguage(new
Language("Action", LanguageTypeEnum.HINDI));
languages.addLanguage(new
Language("Thriller", LanguageTypeEnum.FRENCH));
languages.addLanguage(new
Language("Thriller", LanguageTypeEnum.ENGLISH));
languages.addLanguage(new
Language("Adventure", LanguageTypeEnum.HINDI));
languages.addLanguage(new
Language("Comedy", LanguageTypeEnum.FRENCH));
return languages;
}
}