Example
Dynamic loading is a typical object-oriented feature and prototype example. For example, overriding method is a kind of prototype pattern.
interface Shape {
public void draw();
}
class Line implements Shape {
public void draw() {
System.out.println("line");
}
}
class Square implements Shape {
public void draw() {
System.out.println("square");
}
}
class Circle implements Shape {
public void draw() {
System.out.println("circle");
}
}
We can code the paint method in the following way:
static void paint(Shape s) {
if ( s instanceof Line)
s.draw();
//more job here
if (s instanceof Square)
s.draw();
//more job here
if (s instanceof Circle)
s.draw();
//more job here
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.