<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

    主站蜘蛛池模板: 久久这里只精品热免费99| 高潮毛片无遮挡高清免费视频| 中文字幕在线观看免费| 亚洲精品国自产拍在线观看| 美女一级毛片免费观看| 亚洲精品乱码久久久久久不卡| 最新亚洲人成无码网www电影| 免费人成年激情视频在线观看| 日韩欧美亚洲国产精品字幕久久久| 免费看的成人yellow视频| 亚洲AV成人精品一区二区三区| 免费一级毛片清高播放| 国产福利免费视频 | 亚洲人成电影青青在线播放| 免费看男女下面日出水来| 亚洲毛片基地4455ww| 免费观看毛片视频| 成人嫩草影院免费观看| 亚洲国产精品无码久久一区二区| 日韩精品无码免费一区二区三区 | 亚洲AV色无码乱码在线观看| 免费二级毛片免费完整视频| xxxx日本在线播放免费不卡| 亚洲人成网址在线观看| 成人免费视频试看120秒| 偷自拍亚洲视频在线观看| 国产亚洲精品va在线| 国产精品入口麻豆免费观看| 亚洲精品无码久久久久牙蜜区| 亚洲欧洲国产成人综合在线观看| 久久一本岛在免费线观看2020| 国产亚洲精品bv在线观看| 亚洲精品国产V片在线观看 | 在线免费一区二区| a级毛片免费观看视频| 亚洲国产成人在线视频| 亚洲精品尤物yw在线影院| 在线观看永久免费| 污网站免费在线观看| 亚洲日本香蕉视频| 亚洲国产综合久久天堂|