Composite Design Pattern learning note

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:2091807981; mso-list-type:hybrid; mso-list-template-ids:-1409368958 175392388 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-tab-stop:18.0pt; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

Composite Design Pattern

      

       从其特点可以看出, Composite Design Pattern 非常适合于构造 GUI

Composite Design Pattern learning note_第1张图片


讨论:任何可以看作“控件包含子元素,而每个子元素又可以包含子元素或者作为一个结点”的情况都可以使用这个模式。

Eg.  Menus that contain menu items, each of which could be a menu

     Directories that contain files, each of which could be a directory.

 

CheckList:

1.       Ensure that your problem is about representing “whold-part” hierarchical relationships

2.       Create a “lowest common denominator” interface that makes your containers and containees interchanable. It should specify the behavior that needs to be exercised uniformly across all containee and container objects.

3.       All container and containee classes declare an “is a “ relationship to the interface.

4.       All container classes declare a one-to-many “has a “ relationship to the interface.

5.       Child management methods[e.g. addChild(), removeChild()] should normally be defined in the Composite class. Unfortunately, the desire to treat leaf and Composite objects uniformly may require that these methods be promoted to the abstract Component class. See the GOF for a discussion of these “safety” versus “transparency” trade-offs.

 

 

 

 

 

 

 

 

 

Reference:

http://sourcemaking.com/design_patterns/composite

 

你可能感兴趣的:(list,url,interface,methods,behavior,containers)