<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    Six: Composite pattern

    In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.

    Motivation

    When dealing with Tree-structured data, programmers often have to discriminate between a leaf-node and a branch. This makes code more complex, and therefore, error prone. The solution is an interface that allows treating complex and primitive objects uniformly. In object-oriented programming, a composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar functionality. This is known as a "has-a" relationship between objects.[2] The key concept is that you can manipulate a single instance of the object just as you would manipulate a group of them. The operations you can perform on all the composite objects often have a least common denominator relationship. For example, if defining a system to portray grouped shapes on a screen, it would be useful to define resizing a group of shapes to have the same effect (in some sense) as resizing a single shape.

    [edit]When to use

    Composite can be used when clients should ignore the difference between compositions of objects and individual objects.[1] If programmers find that they are using multiple objects in the same way, and often have nearly identical code to handle each of them, then composite is a good choice; it is less complex in this situation to treat primitives and composites as homogeneous.

    Structure

    Composite pattern in UML.
    Component
    • is the abstraction for all components, including composite ones
    • declares the interface for objects in the composition
    • (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate
    Leaf
    • represents leaf objects in the composition .
    • implements all Component methods
    Composite
    • represents a composite Component (component having children)
    • implements methods to manipulate children
    • implements all Component methods, generally by delegating them to its children

      import java.util.List;
      import java.util.ArrayList;
       
      /** "Component" */
      interface Graphic {
       
          //Prints the graphic.
          public void print();
      }
       
      /** "Composite" */
      class CompositeGraphic implements Graphic {
       
          //Collection of child graphics.
          private List<Graphic> childGraphics = new ArrayList<Graphic>();
       
          //Prints the graphic.
          public void print() {
              for (Graphic graphic : childGraphics) {
                  graphic.print();
              }
          }
       
          //Adds the graphic to the composition.
          public void add(Graphic graphic) {
              childGraphics.add(graphic);
          }
       
          //Removes the graphic from the composition.
          public void remove(Graphic graphic) {
              childGraphics.remove(graphic);
          }
      }
       
      /** "Leaf" */
      class Ellipse implements Graphic {
       
          //Prints the graphic.
          public void print() {
              System.out.println("Ellipse");
          }
      }
       
      /** Client */
      public class Program {
       
          public static void main(String[] args) {
              //Initialize four ellipses
              Ellipse ellipse1 = new Ellipse();
              Ellipse ellipse2 = new Ellipse();
              Ellipse ellipse3 = new Ellipse();
              Ellipse ellipse4 = new Ellipse();
       
              //Initialize three composite graphics
              CompositeGraphic graphic = new CompositeGraphic();
              CompositeGraphic graphic1 = new CompositeGraphic();
              CompositeGraphic graphic2 = new CompositeGraphic();
       
              //Composes the graphics
              graphic1.add(ellipse1);
              graphic1.add(ellipse2);
              graphic1.add(ellipse3);
       
              graphic2.add(ellipse4);
       
              graphic.add(graphic1);
              graphic.add(graphic2);
       
              //Prints the complete graphic (four times the string "Ellipse").
              graphic.print();
          }
      }


    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-11-01 17:10 Chan Chen 閱讀(369) 評論(0)  編輯  收藏 所屬分類: Design Pattern

    主站蜘蛛池模板: 999久久久免费精品国产| 精品亚洲成a人片在线观看| 色婷五月综激情亚洲综合| 久久精品中文字幕免费| 亚洲热妇无码AV在线播放| yy一级毛片免费视频| 久久久久亚洲AV成人网| 国产99视频精品免费视频76 | 99免费视频观看| 亚洲国产无套无码av电影| 一级女性全黄久久生活片免费| 国产v片免费播放| 一级毛片免费视频网站| 亚洲精品中文字幕无码蜜桃| 久青草视频97国内免费影视| 久久久久久久综合日本亚洲| 无码日韩精品一区二区免费暖暖 | 免费看男女下面日出水视频| 深夜a级毛片免费无码| 中文字幕无码精品亚洲资源网| 少妇性饥渴无码A区免费| 久久99亚洲网美利坚合众国| 免费大片黄在线观看yw| 国产成人人综合亚洲欧美丁香花| 国产大片91精品免费看3| 一级看片免费视频| 久久久久久亚洲av成人无码国产| 免费v片在线观看视频网站| 久久人午夜亚洲精品无码区| 久久久久亚洲精品天堂久久久久久| 免费精品久久天干天干| 亚洲一级毛片在线观| 丁香亚洲综合五月天婷婷| 在线看片免费人成视频播| 亚洲一区在线免费观看| 亚洲第一视频在线观看免费| 99精品热线在线观看免费视频| 99热亚洲色精品国产88| 亚洲天堂在线视频| 亚洲视频免费在线播放| 美女视频黄频a免费|