To consider

We need to support not just multiple menus, but even menus within menus. How do you approach this new design requirement? P355

  • 【 Hint 】 In our new design, we really need the following three points:P354
    • We need some kind of property structure that can hold menus, submenus, and menu items
    • We need to make sure we can navigate between items in each menu at least as easily as we can with iterators today
    • We also need to be able to move more flexibly between menu items. For example, you may only need to traverse the dessert menu, or you may be able to traverse the entire restaurant menu, including the dessert menu
  • Provides an interface to unify menus and superclasses of menu items
    • Interfaces contain common methods for menus and menu items, where the execution methods are the same methods in each subitem in turn
    • The interface contains the method of adding, deleting, changing, and checking items unique to the menu. The method of adding, deleting, and changing and checking items in the menu is implemented as a direct throwUnsupportedOperationException
    • Can be achieved byinstanceofDetermine whether the current item is a menu or a menu item

Portfolio model

Allows you to group objects into a tree structure to represent a whole/part hierarchy. Composition enables customers to work with individual objects and groups of objects in a consistent manner.

The characteristics of

  • The same operation is applied to the composition and the individual objects, that is, the difference between the composition and the individual objects can be ignoredP357
  • In exchange for transparency, violate the single responsibility design principle, that is, manage not only the hierarchy, but also include operations to manage both the portfolio and the leaf so that the portfolio and the leaf are treated equallyP367

Empty iterator: An example of an empty object (mentioned in command mode). Empty iterators, hasNext() always returns false, next() always returns null, Remove () throw UnsupportedOperationException forever. P372

To consider

public class Waitress {
    MenuComponent allMenus;
    
    public Waitress(MenuComponent allMenus) {
        this.allMenus = allMenus;
    }
    
    public void printMenu(a) {
        allMenus.print();
    }
    
    public void printVegetarianMenu(a) {
        Iterator iterator = allMenus.createIterator();
        System.out.println("\nVEGETARIAN MENU\n----");
        while (iterator.hasNext()) {
            MenuComponent menuComponent = (MenuComponent)iterator.next();
            try {
                if(menuComponent.isVegetarian()) { menuComponent.print(); }}catch (UnsupportedOperationException e) {}
        }
    }
}
Copy the code

In the printVegetarianMenu() method, only the print() method of the menu item can be called, and the print() method of the menu (combination) must not be called. Can you tell why? P373

  • Iterator traversal already traverses all nodes (including composite nodes and leaf nodes), while composite nodes print information about all child nodesprint(), some leaf nodes must be printed repeatedly.

To consider

Pair the following patterns and descriptions: P379 Policy pattern: Encapsulates interchangeable behavior and uses delegates to determine which adapter pattern to use: Changes the interface iterator pattern for one or more classes: Provides a way to iterate over a collection without exposing the implementation appearance pattern of the collection: Simplifies the interface composition pattern for a group of classes: Clients can treat collections of objects as well as individual objects alike. The observer pattern allows a group of objects to be notified when a state changes

thoughts

  • In many places are used to this kind of thought, trie tree, binary tree, and line segment tree tree structure nodes are allowed to be a tree structure to represent “the whole/part” of the hierarchy, and provides a consistent approach to the leaf nodes and leaf nodes (not a leaf node within a method according to specific logic processing and invoke the same method as child nodes)

This post is posted on GitHub: Reading-Notes/Head-First-Design-Patterns