-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
comprehensive proxy pattern Finished
- Loading branch information
1 parent
27907da
commit 4fe5efe
Showing
38 changed files
with
1,622 additions
and
1 deletion.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
comprehensive/src/main/java/com/luo/composite/menuiterator/CompositeIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.luo.composite.menuiterator; | ||
|
||
import java.util.Iterator; | ||
import java.util.Stack; | ||
|
||
/** | ||
* @author luoxuzheng | ||
* @create 2019-09-30 10:44 | ||
**/ | ||
public class CompositeIterator implements Iterator<MenuComponent> { | ||
|
||
Stack<Iterator<MenuComponent>> stack = new Stack<Iterator<MenuComponent>>(); | ||
|
||
public CompositeIterator(Iterator<MenuComponent> iterator) { | ||
stack.push(iterator); | ||
} | ||
|
||
|
||
@Override | ||
public boolean hasNext() { | ||
if (stack.empty()) { | ||
return false; | ||
} else { | ||
Iterator<MenuComponent> iterator = stack.peek(); | ||
if (!iterator.hasNext()) { | ||
stack.pop(); | ||
return hasNext(); | ||
} else { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public MenuComponent next() { | ||
if (hasNext()) { | ||
Iterator<MenuComponent> iterator = stack.peek(); | ||
MenuComponent component = iterator.next(); | ||
stack.push(component.createIterator()); | ||
return component; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/* | ||
* No longer needed as of Java 8 | ||
* | ||
* (non-Javadoc) | ||
* @see java.util.Iterator#remove() | ||
* | ||
public void remove() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
*/ | ||
} |
65 changes: 65 additions & 0 deletions
65
comprehensive/src/main/java/com/luo/composite/menuiterator/Menu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.luo.composite.menuiterator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* @author luoxuzheng | ||
* @create 2019-09-30 10:39 | ||
**/ | ||
public class Menu extends MenuComponent { | ||
Iterator<MenuComponent> iterator = null; | ||
ArrayList<MenuComponent> menuComponents = new ArrayList<MenuComponent>(); | ||
String name; | ||
String description; | ||
|
||
public Menu(String name, String description) { | ||
this.name = name; | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public void add(MenuComponent menuComponent) { | ||
menuComponents.add(menuComponent); | ||
} | ||
|
||
@Override | ||
public void remove(MenuComponent menuComponent) { | ||
menuComponents.remove(menuComponent); | ||
} | ||
|
||
@Override | ||
public MenuComponent getChild(int i) { | ||
return menuComponents.get(i); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
@Override | ||
public Iterator<MenuComponent> createIterator() { | ||
if (iterator == null) { | ||
iterator = new CompositeIterator(menuComponents.iterator()); | ||
} | ||
return iterator; | ||
} | ||
|
||
@Override | ||
public void print() { | ||
System.out.print("\n" + getName()); | ||
System.out.println(", " + getDescription()); | ||
System.out.println("---------------------"); | ||
|
||
Iterator<MenuComponent> iterator = menuComponents.iterator(); | ||
while (iterator.hasNext()) { | ||
MenuComponent menuComponent = iterator.next(); | ||
menuComponent.print(); | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
comprehensive/src/main/java/com/luo/composite/menuiterator/MenuComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.luo.composite.menuiterator; | ||
|
||
import java.util.Iterator; | ||
|
||
/** | ||
* @author luoxuzheng | ||
* @create 2019-09-30 10:39 | ||
**/ | ||
public abstract class MenuComponent { | ||
|
||
/** | ||
* 增加 | ||
* @param menuComponent | ||
*/ | ||
public void add(MenuComponent menuComponent) { | ||
|
||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* 删除 | ||
* @param menuComponent | ||
*/ | ||
public void remove(MenuComponent menuComponent) { | ||
|
||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* 获取子节点 | ||
* @param i | ||
* @return | ||
*/ | ||
public MenuComponent getChild(int i) { | ||
|
||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* 获取名称 | ||
* @return | ||
*/ | ||
public String getName() { | ||
|
||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* 获取描述 | ||
* @return | ||
*/ | ||
public String getDescription() { | ||
|
||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* 获取价格 | ||
* @return | ||
*/ | ||
public double getPrice() { | ||
|
||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* 是否是蔬菜 | ||
* @return | ||
*/ | ||
public boolean isVegetarian() { | ||
|
||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* 创建迭代器 | ||
* @return | ||
*/ | ||
public abstract Iterator<MenuComponent> createIterator(); | ||
|
||
/** | ||
* 打印 | ||
*/ | ||
public void print() { | ||
|
||
throw new UnsupportedOperationException(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
comprehensive/src/main/java/com/luo/composite/menuiterator/MenuItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.luo.composite.menuiterator; | ||
|
||
import java.util.Iterator; | ||
|
||
/** | ||
* @author luoxuzheng | ||
* @create 2019-09-30 10:39 | ||
**/ | ||
public class MenuItem extends MenuComponent { | ||
|
||
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; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
@Override | ||
public double getPrice() { | ||
return price; | ||
} | ||
|
||
@Override | ||
public boolean isVegetarian() { | ||
return vegetarian; | ||
} | ||
|
||
@Override | ||
public Iterator<MenuComponent> createIterator() { | ||
|
||
return new NullIterator(); | ||
} | ||
|
||
@Override | ||
public void print() { | ||
System.out.print(" " + getName()); | ||
if (isVegetarian()) { | ||
System.out.print("(v)"); | ||
} | ||
System.out.println(", " + getPrice()); | ||
System.out.println(" -- " + getDescription()); | ||
} | ||
|
||
} | ||
|
33 changes: 33 additions & 0 deletions
33
comprehensive/src/main/java/com/luo/composite/menuiterator/MenuItemTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.luo.composite.menuiterator; | ||
|
||
/** | ||
* @author luoxuzheng | ||
* @create 2019-09-30 11:31 | ||
**/ | ||
public class MenuItemTest { | ||
|
||
public static void main(String[] args) { | ||
MenuComponent hotdog = new MenuItem("Hotdog", | ||
"A hot dog, with saurkraut, relish, onions, topped with cheese", | ||
false, | ||
3.05); | ||
|
||
MenuComponent colddog = new MenuItem("colddog", | ||
"A cold dog, with saurkraut, relish, onions, topped with cheese", | ||
true, | ||
3.05); | ||
|
||
|
||
MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined"); | ||
|
||
allMenus.add(hotdog); | ||
allMenus.add(colddog); | ||
|
||
Waitress waitress = new Waitress(allMenus); | ||
|
||
// waitress.printMenu(); | ||
|
||
waitress.printVegetarianMenu(); | ||
} | ||
|
||
} |
Oops, something went wrong.