`
xuchunming
  • 浏览: 30225 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

http://sourcemaking.com/design_patterns/composite/java/2

阅读更多
nterface Component { void traverse(); }      // 1. "lowest common denominator" 

class Primitive implements Component {        // 2. "Isa" relationship
   private int value;
   public Primitive( int val ) { value = val; }
   public void traverse()      { System.out.print( value + "  " ); }
}

abstract class Composite implements Component {      // 2. "Isa" relationship
   private Component[] children = new Component[9];  // 3. Couple to interface
   private int         total    = 0;
   private int         value;
   public Composite( int val )    { value = val; }
   public void add( Component c ) { children[total++] = c; } // 3. Couple to
   public void traverse() {                                  //    interface
      System.out.print( value + "  " );
      for (int i=0; i < total; i++)
          children[i].traverse();            // 4. Delegation and polymorphism 
}  }

class Row extends Composite {                // Two different kinds of "con-
   public Row( int val ) { super( val ); }   // tainer" classes.  Most of the
   public void traverse() {                  // "meat" is in the Composite
      System.out.print( "Row" );             // base class.
      super.traverse();
}  }

class Column extends Composite {
   public Column( int val ) { super( val ); }
   public void traverse() {
      System.out.print( "Col" );
      super.traverse();
}  }

public class CompositeDemo {
   public static void main( String[] args ) {
      Composite first  = new Row( 1 );          // Row1
      Composite second = new Column( 2 );       //   |
      Composite third  = new Column( 3 );       //   +-- Col2
      Composite fourth = new Row( 4 );          //   |     |
      Composite fifth  = new Row( 5 );          //   |     +-- 7
      first.add( second );                      //   +-- Col3
      first.add( third  );                      //   |     |
      third.add( fourth );                      //   |     +-- Row4
      third.add( fifth  );                      //   |     |     |
      first.add(  new Primitive( 6 ) );         //   |     |     +-- 9
      second.add( new Primitive( 7 ) );         //   |     +-- Row5
      third.add(  new Primitive( 8 ) );         //   |     |     |
      fourth.add( new Primitive( 9 ) );         //   |     |     +-- 10
      fifth.add(  new Primitive(10 ) );         //   |     +-- 8
      first.traverse();                         //   +-- 6
}  }

Row1 Col2 7 Col3 Row4 9 Row5 10 8 6 
分享到:
评论

相关推荐

    Beginning Swift Programming.pdf

    Understand the key design patterns in iOS and Mac OS programming using protocols and delegates Learn how to use generics to create highly reusable code Learn the new access controls mechanism in Swift...

    UE(官方下载)

    Regular Expressions are essentially patterns (rather than specific strings) that are used with Find/Replace operations. This guide can dramatically improve your speed and efficiency for Find/Replace ...

    C++学习网站列表.txt

    2. Cygwin(http://sources.redhat.com/cygwin/) Cygwin提供了一个在Windows平台上运行类Unix环境的工具集,包括C++编译器和其他开发工具。 3. KAI C++(http://www.kai.com/) KAI C++是一个高性能的C++编译器...

    Ajax学习 网址备忘录.txt

    - **网址**:http://www.ajaxpatterns.org 和 http://www.softwareas.com/ajax-patterns - **简介**:该网站提供了一系列关于Ajax模式的资源,帮助开发者更好地设计和实现Ajax应用。 - **特色内容**: - 模式库 - ...

    三菱伺服电机选型软件Motorizer(中文):sw1dnn-mz-m_005f.zip.001

    Drive System Sizing Software Motorizer is software that enables you to select the most suitable servo motors for your machine just by ...machine configuration/specifications and the operation patterns....

    《Head First 设计模式》中文完整清晰pdf,配全目录书签,第二分卷

    这是本人过去收集网上的清晰扫描图片,和预览样章合并制成的pdf。由于当时是自已学习用,所以边看边制目录书签。现在放上来希望能攒点分。由于文件有127M大,所以...第七分卷:http://download.csdn.net/source/3000445

    三菱伺服电机选型软件Motorizer(中文):sw1dnn-mz-m_005f.zip.002

    Drive System Sizing Software Motorizer is software that enables you to select the most suitable servo motors for your machine just by ...machine configuration/specifications and the operation patterns....

    Microservices for Java EE Architects-DVT Press(2016).azw3

    Readers of the Java EE Architect’s Handbook, Second Edition are well acquainted with traditional application architectures. Since this book was published, microservices architecture has emerged. It ...

    人脸识别(face recognition)

    在人脸识别方面,OpenCV提供了EigenFace、FisherFace和LBPH(Local Binary Patterns Histograms)三种人脸识别算法。这些算法可以用于训练模型,以识别或验证人脸。例如,使用`cv2.CascadeClassifier`可以检测图像中...

    Web开发者欣喜若狂的40个UI设计工具和资源

    **网址**:http://ui-patterns.com/ #### PatternTap PatternTap是一个收集了广泛设计模板的平台,它将UI元素按类别整理,便于设计师快速找到灵感。每个项目都带有标签,可以在新窗口中单独查看,极大地提高了设计...

    DesignPatterns.pdf_objects_designpatterns_

    标题中的"DesignPatterns.pdf_objects_designpatterns_"暗示了这可能是一个关于面向对象设计模式的PDF文档,而描述中的"Design patterns elements of reusable objects"进一步确认了这一点。设计模式的核心理念在于...

    <<设计模式>>GOF,C++实例源码

    This is my implementation of the Design Patterns from the book 'Design Patterns: Elements of Reusable Object-Oriented Software' Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides ISBN 0-201-...

    fatal error: boostdesc_bgm.i: No such file or directory补充文件

    3. `boostdesc_lbgm.i`: 可能是Local Binary Patterns三阶矩(LBP-TOP)的Boost实现,这是一种用于纹理分类和图像识别的方法。 4. `boostdesc_bgm_bi.i`: 这可能是指Boosting版本的GLOH(Generalized Local Order ...

    expect4j.jar 1.0

    It supports control of processes and sockets, and a complex method of match multiple patterns at the same time. These are needed in some applications, but it complicates the API a tad. Especially ...

    GoF_Design_Patterns_Reference0100.pdf_designpatterns_ebookgof_De

    GOF Design Patterns ebook

    Java教程补充材料

    14 Design Patterns 15 Text I/O Prior to JDK 1.5 (Reader and Writer Classes) 16 Assertions 17 Packaging and Deploying Java Projects 18 Java Web Start 19 Signed Java Applets 20 GridBagLayout ...

    image_thumbnail_proxy:基于nginx+php的懒人缩略图代理

    #缩略图代理作者:sskaje( ) ##Supported URL patterns 来源: http://static.sskaje.me/path/to/image/abcd.jpg 缩略图: ...

    Node.js_Design_Patterns_Second_Edition_Code, node.js 设计模式第二版代码库,由Packt发布.zip

    Node.js_Design_Patterns_Second_Edition_Code, node.js 设计模式第二版代码库,由Packt发布 node.js 设计模式,第二版通过掌握最强大的组件和模式来创建模块化和可以伸缩的应用程序,从而轻松地从 node.js 中。...

    cloud-design-patterns微软中文翻译版本

    cloud-design-patterns微软中文翻译版本,AWS http://en.clouddesignpattern.org/index.php/Main_Page 微软 https://docs.microsoft.com/en-us/azure/architecture/patterns/

    JAVA学习总结与JAVA书籍网站推荐.docx

    - `Concurrent Programming in Java: Design Principles and Patterns` 由Doug Lea撰写,是并发编程的经典之作,有第二版。 - `Refactoring` 由Martin Fowler等人合著,讨论了代码重构的重要性和方法。 - `Design...

Global site tag (gtag.js) - Google Analytics