Nice TWiki > Doc > CodeExamples > VisitorPatternMultiMethodExample > CompositePatternExample (r1.4) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* Please try the NoviceExamples before the MultiMethodExamples
To compile:
   nicec --sourcepath .. -a composite.jar composite
To run:
   java -jar composite.jar
*/


// Composite Pattern: Types and Operations
//----------------------------------------

abstract class Shape {
   boolean containsPoint(Point p);
}

class Square extends Shape { 
   double side; 

   containsPoint(p){
      let d = side/2;
      return 
         (p.x >= -d && p.x < d) && 
         (p.y >= -d && p.y < d);
   }
}

class Circle extends Shape { 
   double radius; 

   // just test the bounding box  
   containsPoint(p) =    
      (p.x >= -radius && p.x < radius) && 
      (p.y >= -radius && p.y < radius);
}

class Translated extends Shape { 
   Point d; 
   Shape shape; 

   containsPoint(p) { 
      let p' = 
         new Point(
            x: p.x - d.x,
            y: p.y - d.y );

      return shape.containsPoint(p');
   }       
}

class Point { double x; double y; }



/*
   =================================================
   Can we add a type and operation without modifying 
   the original code?
   ================================================= 
*/


// Composite Pattern: Adding Type Variants
//----------------------------------------

class Union extends Shape {
   Shape shape1; 
   Shape shape2; 

   containsPoint(Point p) =
      shape1.containsPoint(p) || shape2.containsPoint(p);
}



// Composite Pattern: Adding Operations
//-------------------------------------

/* 
   Generally, in a Composite Pattern we would
   need to modify the original source code.
*/  


// Composite Pattern: Testing
//---------------------------

void main(String[] args){
   let s = new Square(side: 4);
   let c = new Circle(radius: 2);

   let t = 
      new Translated(
         d: new Point(x: 1.5, y: 0),  
         shape: c 
         );

   let p = new Point(x: 3, y: 0);
   println("square contains point " + s.containsPoint(p));
   println("circle contains point " + c.containsPoint(p));
   println("translated contains point " + t.containsPoint(p));

   let u = new Union(shape1: s, shape2: t);
   println("union contains point " + u.containsPoint(p));
}



/* Notes 
See the detailed discussion in
"Synthesizing Object-Oriented and Functional Design to Promote Re-use"
Section 2.2 figures 4, 5, 6
http://citeseer.nj.nec.com/krishnamurthi98synthesizing.html


square contains point false
circle contains point false
translated contains point true
union contains point true

*/
<verbatim>
-- Main.IsaacGouy - 06 Feb 2004
</verbatim>
<nop>

Topic CompositePatternExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.6 | > | r1.5 | > | r1.4 | More }
Revision r1.4 - 23 Jan 2005 - 14:01 GMT - TWikiGuest
Parents: WebHome > CodeExamples > VisitorPatternMultiMethodExample
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.