Java Reference
In-Depth Information
}
And here's the code for Resizable:
public interface Resizable {
int getWidth();
int getHeight();
void setWidth(int width);
void setHeight(int height);
void setAbsoluteSize(int width, int height);
default void setRelativeSize(int wFactor, int hFactor){
setAbsoluteSize(getWidth() / wFactor, getHeight() / hFactor);
}
}
Composing interfaces
You can now create different concrete classes for your game by composing these interfaces. For
example, monsters can be moveable, rotatable, and resizable:
The Monster class will automatically inherit the default methods from the Rotatable, Moveable,
and Resizable interfaces. In this case, Monster inherits the implementations of rotateBy,
moveHorizontally, moveVertically, and setRelativeSize.
You can now call the different methods directly:
Say you now need to declare another class that's moveable and rotatable but not resizable, such
as the sun. There's no need to copy and paste code; you can reuse the default implementations
from the Moveable and Rotatable interfaces as shown here. Figure 9.4 illustrates the UML
diagram of this scenario:
 
Search WWH ::




Custom Search