Skip to content

Commit aada1ae

Browse files
committed
adaptando examplo do HFDP
1 parent 4cedc08 commit aada1ae

File tree

15 files changed

+674
-0
lines changed

15 files changed

+674
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
*.sublime-*
2+
13
# Byte-compiled / optimized / DLL files
24
__pycache__/
35
*.py[cod]

command/java/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Head First Design Patterns
2+
3+
Java code example from chapter 9 of the book [Head First Design Patterns](http://shop.oreilly.com/product/9780596007126.do) (O'Reilly, 2004) by **Eric Freeman**, **Elisabeth Robson**, **Bert Bates** and **Kathy Sierra**.
4+

command/java/menu/Menu.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package headfirst.composite.menu;
2+
3+
import java.util.Iterator;
4+
import java.util.ArrayList;
5+
6+
public class Menu extends MenuComponent {
7+
ArrayList menuComponents = new ArrayList();
8+
String name;
9+
String description;
10+
11+
public Menu(String name, String description) {
12+
this.name = name;
13+
this.description = description;
14+
}
15+
16+
public void add(MenuComponent menuComponent) {
17+
menuComponents.add(menuComponent);
18+
}
19+
20+
public void remove(MenuComponent menuComponent) {
21+
menuComponents.remove(menuComponent);
22+
}
23+
24+
public MenuComponent getChild(int i) {
25+
return (MenuComponent)menuComponents.get(i);
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public String getDescription() {
33+
return description;
34+
}
35+
36+
public void print() {
37+
System.out.print("\n" + getName());
38+
System.out.println(", " + getDescription());
39+
System.out.println("---------------------");
40+
41+
Iterator iterator = menuComponents.iterator();
42+
while (iterator.hasNext()) {
43+
MenuComponent menuComponent =
44+
(MenuComponent)iterator.next();
45+
menuComponent.print();
46+
}
47+
}
48+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package headfirst.composite.menu;
2+
3+
import java.util.*;
4+
5+
public abstract class MenuComponent {
6+
7+
public void add(MenuComponent menuComponent) {
8+
throw new UnsupportedOperationException();
9+
}
10+
public void remove(MenuComponent menuComponent) {
11+
throw new UnsupportedOperationException();
12+
}
13+
public MenuComponent getChild(int i) {
14+
throw new UnsupportedOperationException();
15+
}
16+
17+
public String getName() {
18+
throw new UnsupportedOperationException();
19+
}
20+
public String getDescription() {
21+
throw new UnsupportedOperationException();
22+
}
23+
public double getPrice() {
24+
throw new UnsupportedOperationException();
25+
}
26+
public boolean isVegetarian() {
27+
throw new UnsupportedOperationException();
28+
}
29+
30+
public void print() {
31+
throw new UnsupportedOperationException();
32+
}
33+
}

command/java/menu/MenuItem.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package headfirst.composite.menu;
2+
3+
import java.util.Iterator;
4+
import java.util.ArrayList;
5+
6+
public class MenuItem extends MenuComponent {
7+
String name;
8+
String description;
9+
boolean vegetarian;
10+
double price;
11+
12+
public MenuItem(String name,
13+
String description,
14+
boolean vegetarian,
15+
double price)
16+
{
17+
this.name = name;
18+
this.description = description;
19+
this.vegetarian = vegetarian;
20+
this.price = price;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public String getDescription() {
28+
return description;
29+
}
30+
31+
public double getPrice() {
32+
return price;
33+
}
34+
35+
public boolean isVegetarian() {
36+
return vegetarian;
37+
}
38+
39+
public void print() {
40+
System.out.print(" " + getName());
41+
if (isVegetarian()) {
42+
System.out.print("(v)");
43+
}
44+
System.out.println(", " + getPrice());
45+
System.out.println(" -- " + getDescription());
46+
}
47+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package headfirst.composite.menu;
2+
3+
import java.util.*;
4+
5+
public class MenuTestDrive {
6+
public static void main(String args[]) {
7+
MenuComponent pancakeHouseMenu =
8+
new Menu("PANCAKE HOUSE MENU", "Breakfast");
9+
MenuComponent dinerMenu =
10+
new Menu("DINER MENU", "Lunch");
11+
MenuComponent cafeMenu =
12+
new Menu("CAFE MENU", "Dinner");
13+
MenuComponent dessertMenu =
14+
new Menu("DESSERT MENU", "Dessert of course!");
15+
MenuComponent coffeeMenu = new Menu("COFFEE MENU", "Stuff to go with your afternoon coffee");
16+
17+
MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined");
18+
19+
allMenus.add(pancakeHouseMenu);
20+
allMenus.add(dinerMenu);
21+
allMenus.add(cafeMenu);
22+
23+
pancakeHouseMenu.add(new MenuItem(
24+
"K&B's Pancake Breakfast",
25+
"Pancakes with scrambled eggs, and toast",
26+
true,
27+
2.99));
28+
pancakeHouseMenu.add(new MenuItem(
29+
"Regular Pancake Breakfast",
30+
"Pancakes with fried eggs, sausage",
31+
false,
32+
2.99));
33+
pancakeHouseMenu.add(new MenuItem(
34+
"Blueberry Pancakes",
35+
"Pancakes made with fresh blueberries, and blueberry syrup",
36+
true,
37+
3.49));
38+
pancakeHouseMenu.add(new MenuItem(
39+
"Waffles",
40+
"Waffles, with your choice of blueberries or strawberries",
41+
true,
42+
3.59));
43+
44+
dinerMenu.add(new MenuItem(
45+
"Vegetarian BLT",
46+
"(Fakin') Bacon with lettuce & tomato on whole wheat",
47+
true,
48+
2.99));
49+
dinerMenu.add(new MenuItem(
50+
"BLT",
51+
"Bacon with lettuce & tomato on whole wheat",
52+
false,
53+
2.99));
54+
dinerMenu.add(new MenuItem(
55+
"Soup of the day",
56+
"A bowl of the soup of the day, with a side of potato salad",
57+
false,
58+
3.29));
59+
dinerMenu.add(new MenuItem(
60+
"Hotdog",
61+
"A hot dog, with saurkraut, relish, onions, topped with cheese",
62+
false,
63+
3.05));
64+
dinerMenu.add(new MenuItem(
65+
"Steamed Veggies and Brown Rice",
66+
"Steamed vegetables over brown rice",
67+
true,
68+
3.99));
69+
70+
dinerMenu.add(new MenuItem(
71+
"Pasta",
72+
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
73+
true,
74+
3.89));
75+
76+
dinerMenu.add(dessertMenu);
77+
78+
dessertMenu.add(new MenuItem(
79+
"Apple Pie",
80+
"Apple pie with a flakey crust, topped with vanilla icecream",
81+
true,
82+
1.59));
83+
84+
dessertMenu.add(new MenuItem(
85+
"Cheesecake",
86+
"Creamy New York cheesecake, with a chocolate graham crust",
87+
true,
88+
1.99));
89+
dessertMenu.add(new MenuItem(
90+
"Sorbet",
91+
"A scoop of raspberry and a scoop of lime",
92+
true,
93+
1.89));
94+
95+
cafeMenu.add(new MenuItem(
96+
"Veggie Burger and Air Fries",
97+
"Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
98+
true,
99+
3.99));
100+
cafeMenu.add(new MenuItem(
101+
"Soup of the day",
102+
"A cup of the soup of the day, with a side salad",
103+
false,
104+
3.69));
105+
cafeMenu.add(new MenuItem(
106+
"Burrito",
107+
"A large burrito, with whole pinto beans, salsa, guacamole",
108+
true,
109+
4.29));
110+
111+
cafeMenu.add(coffeeMenu);
112+
113+
coffeeMenu.add(new MenuItem(
114+
"Coffee Cake",
115+
"Crumbly cake topped with cinnamon and walnuts",
116+
true,
117+
1.59));
118+
coffeeMenu.add(new MenuItem(
119+
"Bagel",
120+
"Flavors include sesame, poppyseed, cinnamon raisin, pumpkin",
121+
false,
122+
0.69));
123+
coffeeMenu.add(new MenuItem(
124+
"Biscotti",
125+
"Three almond or hazelnut biscotti cookies",
126+
true,
127+
0.89));
128+
129+
Waitress waitress = new Waitress(allMenus);
130+
131+
waitress.printMenu();
132+
}
133+
}

command/java/menu/Waitress.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package headfirst.composite.menu;
2+
3+
import java.util.Iterator;
4+
5+
public class Waitress {
6+
MenuComponent allMenus;
7+
8+
public Waitress(MenuComponent allMenus) {
9+
this.allMenus = allMenus;
10+
}
11+
12+
public void printMenu() {
13+
allMenus.print();
14+
}
15+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package headfirst.composite.menuiterator;
2+
3+
4+
import java.util.*;
5+
6+
public class CompositeIterator implements Iterator {
7+
Stack stack = new Stack();
8+
9+
public CompositeIterator(Iterator iterator) {
10+
stack.push(iterator);
11+
}
12+
13+
public Object next() {
14+
if (hasNext()) {
15+
Iterator iterator = (Iterator) stack.peek();
16+
MenuComponent component = (MenuComponent) iterator.next();
17+
if (component instanceof Menu) {
18+
stack.push(component.createIterator());
19+
}
20+
return component;
21+
} else {
22+
return null;
23+
}
24+
}
25+
26+
public boolean hasNext() {
27+
if (stack.empty()) {
28+
return false;
29+
} else {
30+
Iterator iterator = (Iterator) stack.peek();
31+
if (!iterator.hasNext()) {
32+
stack.pop();
33+
return hasNext();
34+
} else {
35+
return true;
36+
}
37+
}
38+
}
39+
40+
public void remove() {
41+
throw new UnsupportedOperationException();
42+
}
43+
}
44+
45+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package headfirst.composite.menuiterator;
2+
3+
import java.util.Iterator;
4+
import java.util.ArrayList;
5+
6+
public class Menu extends MenuComponent {
7+
8+
ArrayList menuComponents = new ArrayList();
9+
String name;
10+
String description;
11+
12+
public Menu(String name, String description) {
13+
this.name = name;
14+
this.description = description;
15+
}
16+
17+
public void add(MenuComponent menuComponent) {
18+
menuComponents.add(menuComponent);
19+
}
20+
21+
public void remove(MenuComponent menuComponent) {
22+
menuComponents.remove(menuComponent);
23+
}
24+
25+
public MenuComponent getChild(int i) {
26+
return (MenuComponent)menuComponents.get(i);
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public String getDescription() {
34+
return description;
35+
}
36+
37+
38+
public Iterator createIterator() {
39+
return new CompositeIterator(menuComponents.iterator());
40+
}
41+
42+
43+
public void print() {
44+
System.out.print("\n" + getName());
45+
System.out.println(", " + getDescription());
46+
System.out.println("---------------------");
47+
48+
Iterator iterator = menuComponents.iterator();
49+
while (iterator.hasNext()) {
50+
MenuComponent menuComponent =
51+
(MenuComponent)iterator.next();
52+
menuComponent.print();
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)