| <<O>> Difference Topic CompositePatternExample (r1.6 - 28 Apr 2005 - TWikiGuest) |
| <<O>> Difference Topic CompositePatternExample (r1.5 - 31 Jan 2005 - LiYan) |
| <<O>> Difference Topic CompositePatternExample (r1.4 - 23 Jan 2005 - TWikiGuest) |
| <<O>> Difference Topic CompositePatternExample (r1.3 - 14 Feb 2004 - IsaacGouy) |
| Changed: | |
| < < | // Composite Pattern: Types and Tools //----------------------------------- |
| > > | // Composite Pattern: Types and Operations //---------------------------------------- |
| Changed: | |
| < < |
========================================
Can we add a type and tool without modifying
|
| > > |
=============================================
Can we add a type and operation without modifying
|
| Changed: | |
| < < |
========================================
|
| > > |
=============================================
|
| Changed: | |
| < < | // Composite Pattern: Adding Tools //-------------------------------- |
| > > | // Composite Pattern: Adding Operations //------------------------------------- |
| <<O>> Difference Topic CompositePatternExample (r1.2 - 06 Feb 2004 - IsaacGouy) |
| Changed: | |
| < < | %META:TOPICPARENT{name="VisitorMultiMethodExample"}% |
| > > | %META:TOPICPARENT{name="VisitorPatternMultiMethodExample"}% |
| <<O>> Difference Topic CompositePatternExample (r1.1 - 06 Feb 2004 - IsaacGouy) |
| Added: | |
| > > |
%META:TOPICINFO{author="IsaacGouy" date="1076042444" format="1.0" version="1.1"}%
%META:TOPICPARENT{name="VisitorMultiMethodExample"}%
/* 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 Tools
//-----------------------------------
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 tool 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 Tools
//--------------------------------
/*
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 . { View | Diffs | r1.6 | > | r1.5 | > | r1.4 | More } |
|
Revision r1.1 - 06 Feb 2004 - 04:40 GMT - IsaacGouy Revision r1.6 - 28 Apr 2005 - 11:50 GMT - TWikiGuest |
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. |