Iterator design pattern combination mode

Very happy, this book is a total of 13 chapters, this chapter has been to the 9; also unfortunately, small series out of the books, or do not know how to describe a design pattern. For example, on an iterator with combined mode, the original book long length, small as accessibly some, not the Department, also please the junior partner reference the original book, Xiao Bian also welcome and share with everyone.

There are many ways you can put together as a collection object heap (collection). You can put them into an array, stack, list or hash table, it is your freedom. Each has its own advantages and appropriate use of time, but if you want to iterate these objects, how to do it? Do not worry, the next is to learn how to allow customers to traverse the object but could not spy on your way you store objects; also will learn how to create some super collection of objects, data structures can breath skipped some daunting .

Object Object village Village Restaurant and Pancake House merge the
object-oriented world, small partners are happy not to do, because now they can be in the same place, enjoy a delicious pancake breakfast pancake house, restaurant and delicious lunch a. However, there is little trouble ...

Pancake House menu item is recorded using an array to store, while the restaurant is using the ArrayList record his menu item, the two are not willing to change the manager now realize, after all, there are too many of the code depends on, and we want to reduce dependent, with minimal changes.

Two forms will bring about what issues
would like to know why there are two different menu representations make things complicated, let's try to achieve a simultaneous use of these two menus client code.

We hired a new staff, able to respond to the customer's need to print custom menus, and even tell you whether a menu item is vegetarian, without having to ask the chef. Requirements are as follows:

printMenu (): print out each item on the menu
printBreakfastMenu (): print value breakfast items
printLunchMenu (): print lunch only item
printVegetarianMenu (): print all vegetarian menu items
isItemVegetarian (name): Specifies the name of the item, if the items are vegetarian, then returns true, false otherwise
on implementation, as follows:

All print on every menu item, you must call PancakeHouseMenu and DinerMenu of getMenuItem () method to achieve with their respective individual
PancakeHouseMenu pancakeHouseMenu = new new PancakeHouseMenu ();
ArrayList breakfastItems = pancakeHouseMenu.getMenuItems ();

= New new DinerMenu dinerMenu DinerMenu ();
the MenuItem [] = lunchItems dinerMenu.getMenuItems ();
Now, each specific project inside the print, using the ArrayList breakfast, lunch using arrays
for (int i = 0; i <breakfastItems.size (); ++ I) {
the menuItem menuItem = (the menuItem) breakfastItems.get (I);
of System.out.print (menuItem.getName ());
System.out.println ( "\ T \ T" + menuItem.getPrice ());
the System .out.println ( "\ T" + menuItem.getDescription ());
}

for (int I = 0; I <lunchItems.length; I ++) {
the MenuItem menuItem = lunchItems [I];
of System.out.print (menuItem.getName ());
System.out.println ( "\ T \ T" + menuItem.getPrice ());
System.out.println ( "\ T" + menuItem.getDescription ());
}
implement other methods and approaches are also similar to the method of the page. We always need to deal with two menus and items used to traverse these two cycles. If there is a third restaurant appear in different implementations, we need to have three cycles.
The next step how to do?
They do not want to change their implementation, as means to rewrite a lot of code. So, if we can figure out a way to let them achieve a same menu interface, which is nice too. We try package.

This book gives us the biggest change is part of the package of changes. Changes are happening here: traversing different collection types caused. It can be packaged? We continue to analyze the next:

To traverse breakfast items, we need to use the size ArrayList () and get () method
for (int I = 0; I <breakfastItems.size (); I ++) {
the MenuItem menuItem = (the MenuItem) breakfastItems.get (I);
}
to traverse lunch items, we need to use an array of length field and brackets
for (int i = 0; i <lunchItems.length; i ++) {
MenuItem menuItem = lunchItems [i];
}
now we create an object, we say it iterator (iterator), use it to encapsulate "through each process within the set of objects"
the iterator iterator breakfastMenu.createIterator = ();

the while (iterator.hasNext ()) {
the MenuItem menuItem = (the MenuItem) Iterator.next ();
}
to try it on the array:
the Iterator Iterator lunchMenu.createIterator = ();
the while (iterator.hasNext ()) {
the MenuItem menuItem = (MenuItem) iterator.next ();
}
Shenzhen site construction https://www.sz886.com

Guess you like

Origin blog.csdn.net/chenmh12/article/details/91411497