课程: Python: Design Patterns (2021)
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Composite
- 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 sub-menu items. The sub-menu items can also have their own sub-menu items. Our coding challenge is to display menu and sub-menu 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 or concrete class called Child, inherit 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 a removing them to and from a tree data structure.