Ch10 Interfaces

advertisement
The Need for Flexibility
• Sometimes different classes are really
very similar
e.g. FramedRect and FramedOval
• can be moved
• can check whether a point is inside
• What if we want a variable that sometimes
refers to a rectangle, and at other times an
oval?
Interfaces
• Allow a programmer to exactly describe
the features needed in a particular context
• Allow the use of parameters and variables
that refer to values from more than one
class
Movable
• An interface with two methods:
public interface Movable {
// Move receiver by dx in x direction and dy in y direction
public void move( double dx, double dy );
// Return whether receiver contains point
public boolean contains( Location point );
}
Interfaces as Contracts
Movable geomObject;
– Implies we can call:
• geomObject.move(5, 8);
• geomObject.contains(pressPoint);
• Any object referred to by geomObject
must have the methods move & contains
Interfaces and the classes that
implement them
• Interfaces
– Provide method headers, not method bodies
– May contain constants
• Classes implementing an interface
– Must contain public methods for each method
declared in the interface
– Do not redefine constants in the interface
– May implement multiple interfaces
Implementing Interfaces
public class FunnyFace implements Movable {
private static final int FACE_HEIGHT = 60,
FACE_WIDTH = 60,
EYE_OFFSET = 20,
EYE_RADIUS = 8,
MOUTH_HEIGHT = 10,
MOUTH_WIDTH = FACE_WIDTH/2;
private FramedOval head, leftEye, rightEye, mouth;
public FunnyFace(double left, double top, DrawingCanvas canvas) {
…}
public void move( double dx, double dy ) {
head.move( dx, dy );
leftEye.move( dx, dy );
rightEye.move( dx, dy );
mouth .move( dx, dy );
}
public boolean contains( Location pt ) {
return head.contains( pt );
}
}
Implementing Interfaces
public class StraightFace implements Movable {
private static final int FACE_HEIGHT = 60,
FACE_WIDTH = 60,
EYE_RADIUS = 8,
EYE_OFFSET = 20,
MOUTH_WIDTH = FACE_WIDTH/2;
private FramedOval head;
private Line leftEye, rightEye, nose, mouth;
public StraightFace(double left, double top, DrawingCanvas canvas) {
…}
public void move( double dx, double dy ) {
head.move( dx, dy );
leftEye.move( dx, dy );
rightEye.move( dx, dy );
mouth .move( dx, dy );
nose.move( dx, dy );
}
public boolean contains( Location pt ) {
return head.contains( pt );
}
}
Using Interfaces
Movable face;
face = new FunnyFace(…);
OR
face = new StraightFace(…);
face.move( 12, 13 );
Caveat:
Movable face;
FunnyFace funny;
face = funny; // legal
funny = face; // illegal!
Constants in Interfaces
• Constants are not re-declared in a class
implementing the interface
public interface IntExample {
public static final int DAYS_IN_YEAR = 365;
public static final Location ORIGIN = new Location ( 0, 0 );
}
Using Constants
public class Calendar implements IntExample {
public static final int DAYS_IN_WEEK = 7;
public static final int WEEKS_IN_YEAR =
DAYS_IN_YEAR / DAYS_IN_WEEK;
}
Multiple Interfaces
What if we have two interfaces:
Movable:
public interface Movable {
public void move( double dx, double dy );
public boolean contains( Location point );
}
Colorable:
public interface Colorable {
public void changeColor( Color aColor );
public Color getColor();
}
Implementing Multiple Interfaces
•
HappyFace must define all methods of its interfaces
public class HappyFace implements Movable, Colorable {
public void move(…) {
}
public void contains(…) {
}
public void changeColor(…) {
}
public Color getColor(…) {
}
}
The objectdraw Library
• Uses interfaces extensively
• Examples:
– DrawableInterface
• includes: move, moveTo, hide, show, contains…
– Drawable2DInterface
• includes: methods of DrawableInterface, getWidth, getHeight
– Resizable2DInterface
• includes: methods of Drawable2DInterface, setWidth,
setHeight
Summary of Interfaces
• Can be used as types of variables & parameters
• Include definitions of public constants and
headers of public methods
• Classes
– can implement one or more interfaces
– must provide public method definitions for every
method specified in interfaces
Download