Skip to content

Commit

Permalink
comprehensive proxy pattern Finished
Browse files Browse the repository at this point in the history
  • Loading branch information
RononoaZoro committed Sep 30, 2019
1 parent 27907da commit 4fe5efe
Show file tree
Hide file tree
Showing 38 changed files with 1,622 additions and 1 deletion.
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();
}
*/
}
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();
}
}
}
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();
}
}
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());
}

}

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

}
Loading

0 comments on commit 4fe5efe

Please sign in to comment.