Wrap some objects: make their interfaces look like they are something else. Such a design allows the interface of the class to be converted to the desired interface in order to implement a different interface. Another pattern that wraps objects to simplify their interfaces.
OO adapters play the same role as real-world adapters, transforming one interface into another to meet customer expectations.
public class MenuItem {
String name;
String description;
boolean vegetarian;
double price;
public MenuItem (String name, String description, boolean vegetarian, double price) {
this.name = name;
this.description = description;
this.vegetarian = vegetarian;
this.price = price;
}
// getter and setter
}
public class PancakeHouseMenu {
ArrayList menuItems;
public PancakeHouseMenu () {
menuItems = new ArrayList ();
addItem ("K & B's Pancake Breakfast",
"Pancakes with scrambled eggs, and toast",
true,
2.99);
addItem ("Regular Pancake Breakfast",
"Pancakes with fried eggs, sausage",
false,
2.99);
addItem ("Blueberry Pancakes",
"Pancakes made with fresh blueberries",
true,
3.49);
addItem ("Waffles",
"Waffles, with your choice of blueberries or strawberries",
true,
3.59);
}
public void addItem (String name, String description, boolean vegetarian, double price) {
MenuItem menuItem = new MenuItem (name, description, vegetarian, price);
menuItems.add (menuItem);
}
public ArrayList getMenuItems () {
return menuItems;
}
}
public class DinerMenu {
static final int MAX_ITEMS = 6;
int numberOfItems = 0;
MenuItem [] menuItems;
public DinerMenu () {
menuItems = new MenuItem [MAX_ITEMS];
addItem ("Vegetarian BLT",
"(Fakin ') Bacon with lettuce & tomato on whole wheat", true, 2.99);
addItem ("BLT",
"Bacon with lettuce & tomato on whole wheat", false, 2.99);
addItem ("Soup of the day",
"Soup of the day, with a side of potato salad", false, 3.29);
addItem ("Hotdog",
"A hot dog, with saurkraut, relish, onions, topped with cheese",
false, 3.05);
addItem ("Steamed Veggies and Brown Rice",
"Steamed vegetables over brown rice", true, 3.99);
addItem ("Pasta",
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
true, 3.89);
}
public void addItem (String name, String description, boolean vegetarian, double price) {
MenuItem menuItem = new MenuItem (name, description, vegetarian, price);
if (numberOfItems> MAX_ITEMS) {
System.err.println ("Sorry, menu is full! Can't add item to menu");
} else {
menuItems [numberOfItems] = menuItem;
numberOfItems ++;
}
}
public MenuItem [] getMenuItems () {
return menuItems;
}
}
If we want to print out both Pancakehousemenu and dinermenu, we need two ways of traversing, a list, an array.
This book is a principle that encapsulates the changing parts. What has changed in this example is the traversal caused by different collection types.
Now we create an object that becomes an iterator (Iterator) that encapsulates "the process of traversing each object within the collection."
Iterator Iterator =breakfastmenu.createiterator ();
while (Iterator.hasnext ()) {
MenuItem MenuItem = (MenuItem) iterator.next ();
}
Iterator Iterator =lunchmenu.createiterator ();
while (Iterator.hasnext ()) {
MenuItem MenuItem = (MenuItem) iterator.next ();
}
This way, the traversal is the same.
The first thing about an iterator is that it relies on an interface called an iterator.
Now, let's retrofit the top two menu.
First, define an iterator
public interface Iterator {
boolean hasNext ();
Object next ();
}
public class DinerMenuIterator implements Iterator {
MenuItem [] items;
int position = 0;
public DinerMenuIterator (MenuItem [] items) {
this.items = items;
}
@Override
public boolean hasNext () {
// Because a fixed-length array is used, we not only need to check whether the length of the array is exceeded,
// You must also check if the next item is null, if it is null, it means that there are no other items
if (position> = items.length || items [position] == null) {
return false;
} else {
return true;
}
}
@Override
public Object next () {
MenuItem menuItem = items [position];
position--;
return menuItem;
}
}
Rewrite the restaurant menu with an iterator
public class DinerMenu {
static final int MAX_ITEMS = 6;
int numberOfItems = 0;
MenuItem [] menuItems;
public DinerMenu () {
menuItems = new MenuItem [MAX_ITEMS];
addItem ("Vegetarian BLT",
"(Fakin ') Bacon with lettuce & tomato on whole wheat", true, 2.99);
addItem ("BLT",
"Bacon with lettuce & tomato on whole wheat", false, 2.99);
addItem ("Soup of the day",
"Soup of the day, with a side of potato salad", false, 3.29);
addItem ("Hotdog",
"A hot dog, with saurkraut, relish, onions, topped with cheese",
false, 3.05);
addItem ("Steamed Veggies and Brown Rice",
"Steamed vegetables over brown rice", true, 3.99);
addItem ("Pasta",
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
true, 3.89);
}
public void addItem (String name, String description, boolean vegetarian, double price) {
MenuItem menuItem = new MenuItem (name, description, vegetarian, price);
if (numberOfItems> MAX_ITEMS) {
System.err.println ("Sorry, menu is full! Can't add item to menu");
} else {
menuItems [numberOfItems] = menuItem;
numberOfItems ++;
}
}
public Iterator createIterator () {
// The client does not need to know how the restaurant menu maintains the menu items, nor does it need to know how the iterator is implemented.
// The client simply uses this iterator to iterate through the menu items
return new DinerMenuIterator (menuItems);
}
}
public class PancakeHouseIterator implements Iterator {
ArrayList items;
int position = 0;
public PancakeHouseIterator (ArrayList items) {
this.items = items;
}
@Override
public boolean hasNext () {
if (position> = items.size ()) {
return true;
} else {
return false;
}
}
@Override
public Object next () {
Object object = items.get (position);
position ++;
return object;
}
}
Using iterators to transform Pancakehousemenu
public class Pancakehousemenu {
ArrayList MenuItems;
Public Pancakehousemenu () {
MenuItems = new ArrayList ();
AddItem ("K&b ' s Pancake Breakfast",
"pancakes with scrambled eggs, and toast",
true,
2.99);
AddItem ("Regular pancake Breakfast",
"pancakes with fried eggs, sausage",
false,
2.99);
AddItem ("Blueberry pancakes",
"pancakes made with fresh blueberries",
true,
3.49);
AddItem ("Waffles",
"Waffles, with your choice of blueberries or strawberries",
true,
3.59);
}
public void AddItem (string name, string description, Boolean vegetarian, double price) {
MenuItem MenuItem = new Menu Item (name, description, vegetarian, price);
Menuitems.add (MenuItem);
}
Public Iterator Createiterator () {
return new pancakehouseiterator (MenuItems);
}
Waitress
public class Waitress {
pancakehousemenu pancakehousemenu;
Dinermenu Dinermenu;
Public waitress (Pancakehousemenu Pancakehousemenu, Dinermenu dinermenu) {
This.pancakehousemenu = Pancakehousemenu;
This.dinermenu = Dinermenu;
}
public void Printmenu () {
Iterator pancakeiterator = Pancakehousemenu.createiterator ();
Iterator dineriterator = Dinermenu.createiterator ();
System.out.println ("menu\n----\nbreakfast");
Printmenu (pancakeiterator);
System.out.println ("\nlunch");
Printmenu (Dineriterator);
}
private void Printmenu (Iterator Iterator) {while
(Iterator.hasnext ()) {
MenuItem MenuItem = (MenuItem) Iterator.next ();
System.out.print (Menuitem.getname () + ",");
System.out.print (Menuitem.getprice () + "--");
System.out.println (Menuitem.getdescription ());}}}
Test:
public class Test {public
static void Main (string[] args) {
Pancakehousemenu pancakehousemenu = new Pancakehousem ENU ();
Dinermenu Dinermenu = new Dinermenu ();
Waitress waitress = new Waitress (Pancakehousemenu, dinermenu);
Waitress.printmenu ();
}
}
Results:
Next, let's use iterators in Java.
ArrayList has a method that returns an iterator. We only need to implement an iterator for the restaurant menu because the restaurant menu uses an array, and the array does not support the iterator () method.
Delete the Pancake House menu iterator class, and then, with the import java.util.Iterator in front of the code in the Pancake House menu, change the following line of code:
Public Iterator Createiterator () {
return menuitems.iterator ();
}
The Pancakehousemenu is finished.