composite(转)

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.

   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 mChildGraphics = new ArrayList();
  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: }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值