Design Mode (9): Composite combination mode-Structural Mode

Source: Internet
Author: User

1. Overview

In the data structure, the tree structure is very important. We can apply the tree structure to the design mode.

Example 1: multi-level tree menu.

Example 2: files and folders

 

2. Problem

We can combine simple objects into complex objects, which can be combined into larger objects. We can define simple objects as classes, and then define some container classes to store these simple objects. Client code must distinguish between simple objects and container objects. In fact, most users think they are the same. Different use of these classes makes the program more complex. Recursion is difficult to use. How can we use recursive combinations so that users do not have to distinguish these classes?

3. Solution

Combination Mode: Combine objects into a tree structure to represent the "part-whole" hierarchy. Composite makes the use of a single object and a composite object consistent.

Sometimes it is also called the partial-overall mode, which makes the concept of simple elements and complex elements blurred in the tree structure problem. The customer program can process complex elements like simple elements, this decouples the customer program from the internal structure of complex elements.

The combination mode allows you to optimize the processing of recursive or hierarchical data structures. There are many examples of hierarchical data structures, making the combination mode very useful. A general example of hierarchical data structure is what you encounter every time you use a computer: a file system. The file system consists of directories and files. Content can be installed in each directory. The contents of a directory can be files or directories. In this way, computer file systems are organized in recursive structures. If you want to describe such a data structure, you can use the composite mode.

4. Classification of combination modes

1) define the method for managing child elements in the composite class
2) define the method for managing child elements in the component interface, so that the leaf class needs to implement these methods empty.

5. Applicability

The composite mode is applicable in the following situations:

1) You want to represent the part of the object-the overall hierarchy

2) You want the user to ignore the differences between the composite object and a single object. The user will use all objects in the composite structure in a unified manner.

6. Structure



Shows the typical object structure of c o m p o s I t E:


7. Composition of the build Mode

Abstract component role (Component): it is the object declaration interface in the combination. When appropriate, it implements the default behavior of interfaces common to all classes. Declare an interface to access and manage component sub-parts.

This interface can be used to manage all sub-objects. (Optional) define an interface in a recursive structure to access a parent component and implement it as appropriate.

Leaf component role (leaf): indicates the leaf node object in the composite tree. The leaf node does not have any subnodes. And define the behavior of the element object in the combination.
Composite: defines the behavior of components with child parts. Storage Sub-parts. Perform operations related to sub-parts in the component interface.
Client: The component object is manipulated through the component interface.

8. Results

 

1) • a basic object defined in the class hierarchy that contains basic objects and composite objects can be combined into more complex composite objects, and this composite object can be combined, this keeps recursion. In customer code, you can use composite objects wherever basic objects are used.
2) • Simplified Customer Code customers can use the combination structure and a single object in a consistent manner. Generally, users do not know (or care about) whether a leaf node is processed or a composite component. This simplifies the customer code, because in the classes that define the combination, you do not need to write some functions that are filled with selection statements.
3) • makes it easier to add new types of components. The newly defined composite or leaf subclass automatically works with the existing structure and customer code, and the customer program does not need to change due to the new component class.
4) • making your design more general and easily adding new components will also lead to some problems, that is, it is difficult to limit components in the combination. Sometimes you want a combination to have only some specific components. When using composite, you must check the runtime instead of depending on the type system to apply these constraints.

 

9. Implementation

A typical example is a tree menu. Multi-level display, this menu can add unlimited nodes; the exception is file traversal and so on.

 

  1. <? PHP
  2. /**
  3. * Combination Mode
  4. *
  5. * @ Author guisu
  6. * @ Version 1.0
  7. * Combination mode: tree menu
  8. *
  9. * The object is combined into a tree structure to represent the "part-whole" hierarchy, so that the customer's use of a single object and composite object is consistent.
  10. */
  11. /**
  12. * Abstract component role (Component)
  13. *
  14. */
  15. Abstract class menucomponent
  16. {
  17. Public Function add ($ component ){}
  18. Public Function remove ($ component ){}
  19. Public Function getname (){}
  20. Public Function geturl (){}
  21. Public Function displayoperation (){}
  22. }
  23. /**
  24. * Composite)
  25. *
  26. */
  27. Class menucomposite extends menucomponent
  28. {
  29. Private $ _ items = array ();
  30. Private $ _ name = NULL;
  31. Private $ _ align = '';
  32. Public Function _ construct ($ name ){
  33. $ This-> _ name = $ name;
  34. }
  35. Public Function add ($ component ){
  36. $ This-> _ items [$ component-> getname ()] = $ component;
  37. }
  38. Public Function remove ($ component ){
  39. $ Key = array_search ($ component, $ this-> _ items );
  40. If ($ key! = False) unset ($ this-> _ items [$ key]);
  41. }
  42. Public Function getitems (){
  43. Return $ this-> _ items;
  44. }
  45. Public Function displayoperation (){
  46. Static $ align = '| ';
  47. If ($ this-> getitems ()){
  48. // Substr ($ align, strpos ($ align ,));
  49. $ Align. = '__';
  50. } Else {
  51. $ Align. = '';
  52. }
  53. Echo $ this-> _ name, "<br/> ";
  54. Foreach ($ this-> _ items as $ name => $ item ){
  55. Echo $ align;
  56. $ Item-> displayoperation ();
  57. }
  58. }
  59. Public Function getname (){
  60. Return $ this-> _ name;
  61. }
  62. }
  63. /**
  64. * Leaf component role (leaf)
  65. *
  66. */
  67. Class itemleaf extends menucomponent
  68. {
  69. Private $ _ name = NULL;
  70. Private $ _ url = NULL;
  71. // Public $ _ align = '----';
  72. Public Function _ construct ($ name, $ URL)
  73. {
  74. $ This-> _ name = $ name;
  75. $ This-> _ url = $ URL;
  76. }
  77. Public Function displayoperation ()
  78. {
  79. Echo '<a href = "', $ this-> _ URL, '">', $ this-> _ name, '</a> <br/> ';
  80. }
  81. Public Function getname (){
  82. Return $ this-> _ name;
  83. }
  84. }
  85. Class Client
  86. {
  87. Public static function displaymenu ()
  88. {
  89. $ Submenu1 = new menucomposite ("submenu1 ");
  90. $ Submenu2 = new menucomposite ("submenu2 ");
  91. $ Submenu3 = new menucomposite ("submenu3 ");
  92. $ Submenu4 = new menucomposite ("submenu4 ");
  93. $ Submenu5 = new menucomposite ("submenu5 ");
  94. /*
  95. $ Item1 = new itemleaf ("Sohu", "www.163.com ");
  96. $ Item2 = new itemleaf ("Sina", "www.sina.com ");
  97. $ Submenu4 = new menucomposite ("submenu4 ");
  98. $ Submenu1-> Add ($ submenu4 );
  99. $ Submenu4-> Add ($ Item1 );
  100. $ Submenu4-> Add ($ item2 );
  101. */
  102. $ Item3 = new itemleaf ("Baidu", "www.baidu.com ");
  103. $ Item4 = new itemleaf ("google", "www.google.com ");
  104. $ Submenu2-> Add ($ item3 );
  105. $ Submenu2-> Add ($ item4 );
  106. $ Allmenu = new menucomposite ("allmenu ");
  107. $ Allmenu-> Add ($ submenu1 );
  108. $ Allmenu-> Add ($ submenu2 );
  109. $ Allmenu-> Add ($ submenu3 );
  110. $ Submenu3-> Add ($ submenu4 );
  111. $ Submenu4-> Add ($ submenu5 );
  112. $ Allmenu-> displayoperation ();
  113. }
  114. }
  115. // Create a menu
  116. Client: displaymenu ();
  117. ?>


 

 

10. Combination Mode and other related modes

1) The decorator mode is often used with the composite mode. When decoration and combination are used together, they

There is usually a public parent class. Therefore, decoration must support the component interfaces with add, remove, and getchild operations.

2) The flyweight mode allows you to share components, but you can no longer reference their parent components.

3) (iterator mode) itertor can be used to traverse composite.

4) (Observer mode) visitor will be originally distributed in composite and l e a f Class for operations and behavior localization.

11. Conclusion

The combination mode decouples the internal structure of the customer program and complex elements, so that the customer program can process complex elements like simple elements.

If you want to create a hierarchy and treat all elements in the same way, the combination mode is the best choice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.