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

    主站蜘蛛池模板: 亚洲午夜爱爱香蕉片| 亚洲a∨国产av综合av下载| 亚洲天堂免费在线视频| 免费人成在线视频| 无码人妻精品中文字幕免费| 丰满妇女做a级毛片免费观看| 亚洲真人无码永久在线观看| 久久丫精品国产亚洲av不卡| 亚洲精品乱码久久久久久自慰| 亚洲 综合 国产 欧洲 丝袜| 午夜一级毛片免费视频| 两性刺激生活片免费视频| 无码精品人妻一区二区三区免费看| 一级一级毛片免费播放| 美女一级毛片免费观看| 亚洲精品国产综合久久久久紧| 亚洲AV综合色区无码二区爱AV| 久久久久亚洲AV无码网站| 国产∨亚洲V天堂无码久久久| 久久精品国产亚洲AV不卡| 午夜亚洲福利在线老司机| 亚洲第一成人影院| 亚洲乱码中文字幕手机在线| 亚洲AⅤ永久无码精品AA| 亚洲AV日韩精品一区二区三区| 国产精品免费视频一区| 国产在线a不卡免费视频| 国产在线观看免费视频播放器 | 亚洲AV人无码综合在线观看| 国产亚洲AV手机在线观看| 国产亚洲精品久久久久秋霞| 亚洲日产韩国一二三四区| 亚洲精品狼友在线播放| 亚洲av综合色区| 亚洲美女视频一区二区三区| 97se亚洲综合在线| 亚洲一级特黄特黄的大片| 亚洲色偷精品一区二区三区| 久久无码av亚洲精品色午夜| 特级av毛片免费观看| aa级女人大片喷水视频免费|