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

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

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

    人在江湖

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

    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 "[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.

       1: import java.util.ArrayList;
       2:  
       3: /** "Component" */
       4: interface Graphic {
       5:  
       6:     //Prints the graphic.
       7:     public void print();
       8: }
       9:  
      10: /** "Composite" */
      11: class CompositeGraphic implements Graphic {
      12:  
      13:     //Collection of child graphics.
      14:     private List<Graphic> mChildGraphics = new ArrayList<Graphic>();
      15:  
      16:     //Prints the graphic.
      17:     public void print() {
      18:         for (Graphic graphic : mChildGraphics) {
      19:             graphic.print();
      20:         }
      21:     }
      22:  
      23:     //Adds the graphic to the composition.
      24:     public void add(Graphic graphic) {
      25:         mChildGraphics.add(graphic);
      26:     }
      27:  
      28:     //Removes the graphic from the composition.
      29:     public void remove(Graphic graphic) {
      30:         mChildGraphics.remove(graphic);
      31:     }
      32: }
      33:  
      34: /** "Leaf" */
      35: class Ellipse implements Graphic {
      36:  
      37:     //Prints the graphic.
      38:     public void print() {
      39:         System.out.println("Ellipse");
      40:     }
      41: }
      42:  
      43: /** Client */
      44: public class Program {
      45:  
      46:     public static void main(String[] args) {
      47:         //Initialize four ellipses
      48:         Ellipse ellipse1 = new Ellipse();
      49:         Ellipse ellipse2 = new Ellipse();
      50:         Ellipse ellipse3 = new Ellipse();
      51:         Ellipse ellipse4 = new Ellipse();
      52:  
      53:         //Initialize three composite graphics
      54:         CompositeGraphic graphic = new CompositeGraphic();
      55:         CompositeGraphic graphic1 = new CompositeGraphic();
      56:         CompositeGraphic graphic2 = new CompositeGraphic();
      57:  
      58:         //Composes the graphics
      59:         graphic1.add(ellipse1);
      60:         graphic1.add(ellipse2);
      61:         graphic1.add(ellipse3);
      62:  
      63:         graphic2.add(ellipse4);
      64:  
      65:         graphic.add(graphic1);
      66:         graphic.add(graphic2);
      67:  
      68:         //Prints the complete graphic (four times the string "Ellipse").
      69:         graphic.print();
      70:     }
      71: }
    posted on 2011-02-13 20:30 人在江湖 閱讀(704) 評論(0)  編輯  收藏 所屬分類: design pattern
    主站蜘蛛池模板: 免费国产不卡午夜福在线 | 亚洲无圣光一区二区| 国产一级黄片儿免费看| 亚洲精品国产V片在线观看| 一级毛片正片免费视频手机看| 国产免费黄色大片| 成人午夜免费视频| 亚洲区小说区图片区QVOD| 97国免费在线视频| 亚洲va久久久噜噜噜久久| 无码日韩精品一区二区免费暖暖| 亚洲成AV人在线观看天堂无码| 国产一精品一av一免费爽爽 | 亚洲欧美日韩自偷自拍| 韩国二级毛片免费播放| 免费的黄色的网站| 亚洲桃色AV无码| 最近免费中文字幕mv电影| 亚洲天堂2017无码中文| 可以免费观看一级毛片黄a| 成人自慰女黄网站免费大全| 五月天网站亚洲小说| 久久笫一福利免费导航| 色窝窝亚洲AV网在线观看| 国产成人精品曰本亚洲79ren| 久久久久国产免费| 亚洲精品一卡2卡3卡四卡乱码| 精品久久久久久久免费人妻| 国产精品极品美女自在线观看免费 | 亚洲国产精品无码久久一区二区 | 日本一道本不卡免费 | 亚洲中文字幕无码mv| 免费一看一级毛片| 国产激情免费视频在线观看| 亚洲国产精品综合久久网各| 免费在线观看a级毛片| 成人性做爰aaa片免费看| 亚洲伊人久久大香线蕉| 亚洲美日韩Av中文字幕无码久久久妻妇| 成在人线av无码免费高潮喷水 | 日韩一区二区三区免费播放|