Head first design pattern -- 11. Composite pattern

Man Fu Zhu Ji 2021-01-14 21:05:38
head design pattern composite pattern


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

版权声明
本文为[Man Fu Zhu Ji]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210114210455867a.html

  1. springboot异常处理之404
  2. Spring boot security international multilingual I18N
  3. Spring boot exception handling 404
  4. Netty系列化之Google Protobuf编解码
  5. Netty之编解码
  6. Java编解码
  7. Netty解码器
  8. Netty与TCP粘包拆包
  9. Netty开发入门
  10. Java集合遍历时遇到的坑
  11. Spring IOC 源码解析(下)
  12. Spring IoC源码解析(上)
  13. Google protobuf codec of netty serialization
  14. Encoding and decoding of netty
  15. Java codec
  16. Netty decoder
  17. Netty and TCP packet sticking and unpacking
  18. Introduction to netty development
  19. Problems encountered in Java collection traversal
  20. Spring IOC source code analysis (2)
  21. Spring IOC source code analysis (Part one)
  22. 半小时用Spring Boot注解实现Redis分布式锁
  23. Implementing redis distributed lock with spring boot annotation in half an hour
  24. What should we do if we can't get tickets for Spring Festival transportation? You can solve this problem by using these ticket grabbing apps!
  25. 百度智能(文本识别),API传图OC代码与SDK使用
  26. springboot源码解析-管中窥豹系列之aware(六)
  27. Baidu intelligent (text recognition), API map, OC code and SDK
  28. Spring boot source code analysis
  29. springboot源码解析-管中窥豹系列之aware(六)
  30. 百度智能(文本识别),API传图OC代码与SDK使用
  31. Spring boot source code analysis
  32. Baidu intelligent (text recognition), API map, OC code and SDK
  33. Java学习笔记
  34. Java learning notes
  35. Sentry(v20.12.1) K8S 雲原生架構探索, SENTRY FOR JAVASCRIPT 手動捕獲事件基本用法
  36. 我的程式設計師之路:自學Java篇
  37. SpringBoot專案,如何優雅的把介面引數中的空白值替換為null值?
  38. Sentry (v20.12.1) k8s cloud native architecture exploration, sentry for JavaScript manual capture event basic usage
  39. My way of programmer: self study java
  40. Spring boot project, how to gracefully replace the blank value in the interface argument with null value?
  41. Redis 用的很溜,了解过它用的什么协议吗?
  42. Redis is easy to use. Do you know what protocol it uses?
  43. 《零基础看得懂的C++入门教程 》——(10)面向对象
  44. Introduction to zero basic C + + (10) object oriented
  45. HTTP status code and troubleshooting
  46. Java NIO之Channel(通道)入门
  47. Introduction to Java NiO channel
  48. Spring中的@Valid 和 @Validated注解你用对了吗
  49. Are you using the @ valid and @ validated annotations correctly in spring
  50. Spring中的@Valid 和 @Validated注解你用对了吗
  51. Are you using the @ valid and @ validated annotations correctly in spring
  52. Redis | 慢查询
  53. Redis | slow query
  54. RabbitMQ一个优秀的.NET消息队列框架
  55. Autofac一个优秀的.NET IoC框架
  56. 如何使用Redis实现分布式缓存
  57. Rabbitmq an excellent. Net message queue framework
  58. Autofac is an excellent. Net IOC framework
  59. How to use redis to realize distributed cache
  60. JDK1.7-HashMap原理