Question

In: Computer Science

Use the iterator pattern to design and code a software (program) for some meaningful application (different...

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!

Solutions

Expert Solution

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;
   }

}

  
  

Related Solutions

DESIGN A FLOWCHART IN THE APPLICATION FLOWGORITHM Exercise called: Number Analysis Program Design a program that...
DESIGN A FLOWCHART IN THE APPLICATION FLOWGORITHM Exercise called: Number Analysis Program Design a program that asks the user to enter a maximum of 20 numbers. The program should store the numbers in an array and then display the following data: 1-The lowest number in the array. 2-The highest number in the array. 3-The total of the numbers in the array. 4-The average of the numbers in the array. PLEASE AND THANK YOU
Three firms can supply a college with some application software. Each firm has a different pricing...
Three firms can supply a college with some application software. Each firm has a different pricing structure: the first firm provides a site license which costs $130,000 and can be used by anyone at the college. The second firm charges $1000 per user and the third charges a fixed amount of $40,000 for the first 60 users and $500 for each additional user. Draw a graph of each cost function on the same set of axes. What advice can you...
I. Design the C++ code for a program which will use the Pythagorean Theroem to calculate...
I. Design the C++ code for a program which will use the Pythagorean Theroem to calculate and display the length of a right triangle hypotenuse,c, given the lengths of its adjacent sides: a and b.. (hint: a^2 + b^2 = c^2) II. Design the C++ code for a program which will calculate and display the roots of a quadratic equation of the form ax^2 + bx + c = 0, given its coefficients, a, b, and c.
You have had an opportunity to use two application software packages to complete three different projects....
You have had an opportunity to use two application software packages to complete three different projects. Name the two application packages. For each package, define its primary purpose and provide two examples of how you would use the product in either a personal or a business setting.
Software Design When is the model-view-controller pattern better than the client-server pattern? Why? Justify your answer.
Software Design When is the model-view-controller pattern better than the client-server pattern? Why? Justify your answer.
You are asked to design a relational database for a simple course registration software application for...
You are asked to design a relational database for a simple course registration software application for your school. The relational database must have the following information about the student, the course, and the registration, respectively StudentID, FirstName, LastName, DataOfJoining, and Major CourseNumber, CourseName,InstructorName, StartDate, EndDate, NumberOfCredits ReferenceID, StudentID,CourseID, DateOfRegistration Apply the following constrains while designing the database Each student in the database must be uniquely identifiable Each course listed in the database must be have unique CourseNumber Each course registration...
Design microstrip circular array antenna in HFSS software and MATLAB CODE FOR IT. Design microstrip circular...
Design microstrip circular array antenna in HFSS software and MATLAB CODE FOR IT. Design microstrip circular array antenna in HFSS software and MATLAB CODE FOR IT. IF YOU KNOW THEN ONLY DO OR ELSE LEAVE FOR OTHER ANTENNA ,ELECTRICAL ENGINEERING
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public...
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public class Main { public static void main(String[] args) { /* Let's create a family tree (for instance like one used on genealogy sites). For the sake of simplicity, assume an individual can have at most two children. If an individual has 1-2 children, they are considered a "tree". If an individual does not have children, they are considered a "person". With that in mind,...
Your software development company wants to continue developing and enhancing a software application for internal use....
Your software development company wants to continue developing and enhancing a software application for internal use. For this week's assignment, the application is required to read from a file (data.txt) containing employee data. Your tasks include: Use the data file from Week One (data.txt) Implement LINQ functionalities to search, display, and modify employees' records Meet specifications by displaying on a Windows Form Application Program Input File: Using Visual Studio and C# programming concepts and using your Week one data.txt file...
Write a program to validate Canadian Postal Codes. A postal code must follow the pattern of...
Write a program to validate Canadian Postal Codes. A postal code must follow the pattern of L9L9L9 where: L is a letter 9 is a digit Your program should continue accepting postal codes until the user enters the word “exit”. Sample run (user input is shown in bold underline): Enter a postal code: T2T-3X7 Postal code wrong length, format L9L9L9 Enter a postal code: T2T3AA Postal code has letters where there are supposed to be digits Enter a postal code:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT