<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 閱讀(366) 評論(0)  編輯  收藏 所屬分類: Design Pattern

    主站蜘蛛池模板: 亚洲精品天堂成人片AV在线播放 | 久久精品国产亚洲AV麻豆不卡 | 特级做a爰片毛片免费看| 亚洲乱色熟女一区二区三区丝袜| 亚洲精品无码久久久久A片苍井空| 亚洲免费网站在线观看| 亚洲精品一卡2卡3卡三卡四卡| 免费国产黄网站在线观看视频| 国产精品成人免费福利| 亚洲成人高清在线观看| 日本一区二区在线免费观看| 免费一级毛片免费播放| 免费在线观看亚洲| 亚洲中文字幕成人在线| 亚洲制服丝袜第一页| 久久亚洲精品无码网站| 成年男女男精品免费视频网站 | 免费人成网站永久| 亚洲色偷偷综合亚洲AVYP| 你懂得的在线观看免费视频| 99re热免费精品视频观看| 亚洲日韩v无码中文字幕| 免费看搞黄视频网站| 亚洲国产午夜精品理论片| a一级毛片免费高清在线| 亚洲啪啪AV无码片| 国产精彩免费视频| 久久久久亚洲AV无码去区首| 国产亚洲色婷婷久久99精品91| 日韩免费高清大片在线| 2019亚洲午夜无码天堂| 日韩精品电影一区亚洲| 99精品视频在线免费观看| 亚洲成aⅴ人片久青草影院按摩 | 亚洲国产高清视频| 四虎www成人影院免费观看| 国产免费内射又粗又爽密桃视频| 亚洲第一成年网站大全亚洲| 免费人成在线观看网站品爱网日本 | 浮力影院亚洲国产第一页| 四虎在线最新永久免费|