removing responsibilities from model to presenter

public class MenuPresenter { private MenuModel menu; private Formatter formatter; //Listeners private ArrayList<ChangeListener> listeners; public MenuPresenter(){ //initialize everything needed } // Text to be sent to the screen public String display(){ StringBuilder sb = new StringBuilder(); sb.append(formatter.formatHeader()); Iterator<MenuItem> iter = menu.getItems(); while(iter.hasNext()) sb.append(formatter.formatOrder(; sb.append(formatter.formatFooter()); return sb.toString(); } public void add(MenuItem menuItem){ menu.add(menuItem) this.changeState(; } public void clear(){ menu.clear(); this.changeState(; } private void changeState(MenuModel menu){ ChangeEvent event = new ChangeEvent(menu); for(ChangeListener lister : listeners) lister.stateChanged(event); } public void addChangeListener(ChangeListener listener){ listeners.add(listener); } } //Here you can change the source of the content, a DAO could implement this, or a file reader or any source of data :) public interface MenuReader{ public List<MenuItem> read(); } public class MenuModel { // ArrayList hold MenuItem private ArrayList<MenuItem> menuItems; // Use to read textfile private MenuReader menuReader; // Data to keep menu's price and the menu's order //Keep for what was ordered private ArrayList<MenuItem> order; /** * * @param file name */ public MenuModel(MenuReader reader){ order = new ArrayList<MenuItem>(); menuItems =; } // update the menu & price public void add(MenuItem menuItem){ order.add(menuItem); } /** * * @return Menu Order Size */ public int size(){ return menuOrder.size(); } // clear price and menu public void clear(){ order.clear(); } // Possible order available public ArrayList<MenuItem> getMenuItem(){ return menuItems; } // Get what was ordered public ArrayList<MenuItem> getOrderList(){ return order; } // I really don't know why you would need to use iterators :( public Iterator<MenuItem> getItems(){ return new Iterator<MenuItem>(){ private int current = 0; @Override public boolean hasNext(){ return current < order.size(); } @Override public MenuItem next() { return order.get(current++); } @Override public void remove() { throw new UnsupportedOperationException(); } }; } }

2 Responses


Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.