For the duck simulation game Developer Designed following OO Design
HAS relationship comes from inheritance.
IS relationship comes from composition.
So the statement is equivalent to saying "Composition over inheritance".
Let's examine using the following case study:
=================================
Point class:
---------------
public class Point{
private int x;
private int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o){
if(!o instance of Point)
return false;
Point p = (Point)o;
return p.x == x && p.y == y;
}
}
ColorPoint class:
----------------------
public class ColorPoint extends Point {
private final Color color;
public ColorPoint(int x, int y, Color color) {
super(x, y);
this.color = color;
}
}
// Broken - violates symmetry!
@Override public boolean equals(Object o) {
if (!(o instanceof ColorPoint))
return false;
return super.equals(o) && ((ColorPoint) o).color == color;
}
Point p = new Point(1, 2);
ColorPoint cp = new ColorPoint(1, 2, Color.RED);
p.equals(cp) => true
cp.equals(p) => false.
Thus symmetry is broken. Lets deal the problem with choosing composition:
ColorPoint class:
----------------------
private Color color;
public ColorPoint1(int x, int y, Color color) {
this.point = new Point(x,y);
this.color = color;
}
public Point asPoint(){
return this.point;
}
@Override
public boolean equals(Object o){
if(!(o instanceof ColorPoint))
return false;
ColorPoint1 cp = (ColorPoint1)o;
return cp.point.equals(o) && cp.color==color;
}
}
now since ColorPoint doesn't extend Point, it's instance is no longer an instance of Point
hence p.equals(cp) => false
also cp.equals(p) => false
and thus symmetry is preserved.
The above example proves the power of composition over inheritance.
HAS relationship comes from inheritance.
IS relationship comes from composition.
So the statement is equivalent to saying "Composition over inheritance".
Let's examine using the following case study:
=================================
Point class:
---------------
public class Point{
private int x;
private int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o){
if(!o instance of Point)
return false;
Point p = (Point)o;
return p.x == x && p.y == y;
}
}
ColorPoint class:
----------------------
public class ColorPoint extends Point {
private final Color color;
public ColorPoint(int x, int y, Color color) {
super(x, y);
this.color = color;
}
}
// Broken - violates symmetry!
@Override public boolean equals(Object o) {
if (!(o instanceof ColorPoint))
return false;
return super.equals(o) && ((ColorPoint) o).color == color;
}
Point p = new Point(1, 2);
ColorPoint cp = new ColorPoint(1, 2, Color.RED);
p.equals(cp) => true
cp.equals(p) => false.
Thus symmetry is broken. Lets deal the problem with choosing composition:
ColorPoint class:
----------------------
public class ColorPoint {
private final Point point;private Color color;
public ColorPoint1(int x, int y, Color color) {
this.point = new Point(x,y);
this.color = color;
}
public Point asPoint(){
return this.point;
}
@Override
public boolean equals(Object o){
if(!(o instanceof ColorPoint))
return false;
ColorPoint1 cp = (ColorPoint1)o;
return cp.point.equals(o) && cp.color==color;
}
}
now since ColorPoint doesn't extend Point, it's instance is no longer an instance of Point
hence p.equals(cp) => false
also cp.equals(p) => false
and thus symmetry is preserved.
The above example proves the power of composition over inheritance.
No comments:
Post a Comment