Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

Head first design pattern -- 11. Composite pattern

Man Fu Zhu Ji 2021-01-14 21:05:38 阅读数:3 评论数:0 点赞数:0 收藏数:0

Thinking questions

We don't just have to support multiple menus , We also support the menu in the menu . How do you deal with this new design requirement ? P355

  • 【 Tips 】 In our new design , The following three points are really needed : P354
    • We need some kind of attribute structure , You can hold a menu 、 Submenus and menu items
    • We need to be sure to be able to swim between items in each menu , And at least as easy as using an iterator now
    • We also need to be able to move more flexibly between menu items . For example , Maybe you just need to go through the dessert menu , Or you can go through the restaurant's entire menu ( Including dessert menu )
  • Provide an interface , Unify the superclass of menu and menu item
    • Interface contains common methods for menus and menu items , The execution method in the menu is to execute the same method in each subitem in turn
    • Interface contains menu specific methods of adding, deleting, modifying and checking subitems , The method of adding, deleting and checking subitems in menu items is to throw out directly UnsupportedOperationException
    • Can pass instanceof Determine whether the current item is a menu or a menu item

Portfolio model

Allows you to combine objects into a tree structure to represent ” whole / part “ hierarchy . Composition allows customers to handle individual objects and composition of objects in a consistent way .
 Portfolio model

characteristic

  • Apply the same operations to composite and individual objects , That is, you can ignore the differences between object combinations and individual objects P357
  • In exchange for transparency by violating the principle of Single Responsibility Design , It's not just about managing hierarchies , It also includes the operation of managing composition and leaf nodes , To treat composition and leaf nodes alike P367

Empty the iterator : An empty object ( The command mode mentioned ) An example of . Empty the iterator , hasNext() Never return false , next() Never return null ( I think it can be thrown out NoSuchElementException), remove() Always throw out UnsupportedOperationException . P372

Thinking questions

public class Waitress {
MenuComponent allMenus;
public Waitress(MenuComponent allMenus) {
this.allMenus = allMenus;
}
public void printMenu() {
allMenus.print();
}
public void printVegetarianMenu() {
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) {}
}
}
}

printVegetarianMenu() Method has only menu items print() Methods can be called , Never call the menu ( Combine ) Of print() Method . Can you tell me why ? P373

  • When using iterator traversal, all nodes will be traversed ( Including composite nodes and leaf nodes ), And the composite node will print all the sub node information , If you call the print() , Some leaf nodes will be printed repeatedly .

Thinking questions

Match the following patterns and descriptions : P379
The strategy pattern : Encapsulating interchangeable behavior , And use delegation to decide which one to use
Adapter pattern : Change the interface of one or more classes
Iterator pattern : Provides a way to traverse the collection , Without exposing the implementation of the collection
Appearance mode : Simplify the interface of a group of classes
Portfolio model : Customers can treat a collection of objects as well as individual objects equally
Observer mode : When a state changes , Allow a group of objects to be informed of

Thinking and thinking

  • This idea has been used in many places before , Binary tree 、trie Tree structure and line tree structure allow nodes to form a tree structure to represent ” whole / part “ Hierarchical structure , And provide a consistent way to deal with non leaf nodes and leaf nodes ( Inside the method of a non leaf node, it processes and calls the same method of a child node according to specific logic )

This article was first published on the official account : Man Fu Zhu Ji ( Click to view the original ) Open source in GitHub :reading-notes/head-first-design-patterns

Copyright statement
In this paper,the author:[Man Fu Zhu Ji],Reprint please bring the original link, thank you