<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

    主站蜘蛛池模板: a毛片成人免费全部播放| 香蕉大伊亚洲人在线观看| yellow视频免费看| 亚洲成?v人片天堂网无码| 国产亚洲精品2021自在线| 国产成人aaa在线视频免费观看 | 亚洲视频在线观看免费| 中文字幕不卡免费视频| 亚洲啪啪综合AV一区| 男人都懂www深夜免费网站| 亚洲gv白嫩小受在线观看| 免费无遮挡无码永久视频| 亚洲精品美女久久久久| AV无码免费永久在线观看| 亚洲综合激情五月色一区| 波多野结衣久久高清免费 | 亚欧免费视频一区二区三区| 亚洲精品视频免费看| 久久不见久久见免费影院| 亚洲av永久中文无码精品综合| 国产麻豆剧传媒精品国产免费| 老司机福利在线免费观看| 在线观看亚洲精品福利片| 无码AV片在线观看免费| 亚洲av无码电影网| 免费v片在线观看品善网| 插鸡网站在线播放免费观看| 亚洲影院在线观看| 国外成人免费高清激情视频| 一级毛片a女人刺激视频免费| 久久精品国产亚洲AV麻豆~| 无码国产精品一区二区免费式直播 | 亚洲人成网站在线观看青青| 日本免费污片中国特一级| 亚洲国产人成在线观看| 国产男女性潮高清免费网站| 国产一级婬片A视频免费观看| 亚洲福利视频一区二区三区| 国产又黄又爽又刺激的免费网址| 两个人日本WWW免费版| 33333在线亚洲|