课程: Python: Design Patterns

免费学习该课程!

今天就开通帐号,24,700 门业界名师课程任您挑!

Composite

Composite

- [Instructor] The composite design pattern maintains a tree data structure to represent part whole relationships. Here we want to build a recursive tree data structure so that an element of the tree can have its own sub-elements. An example of this problem is creating menu and submenu items. The submenu items can also have their own submenu items. Our coding challenge is to display menu and submenu items using the composite design pattern. Our solution consists of three major elements. The first one is component, the second one is child, and the third one is composite. The component element is an abstract class. A concrete class called child inherits from the component class. And then we have another concrete class called composite, which also inherits from the component class. Finally, our composite class maintains child objects by adding and removing them to and from a tree data structure.

内容