CMPUT 301

Software Engineering

Practice w23hm1q3


Liskov Substitution Principle

This Java code is poor! It violates the Liskov Substitution principle!

Restructure this code as UML and draw a UML Class Diagram of restructured code that doesn't violate the Liskov Substitution principle.

Draw a well-designed UML class diagram to represent this information. Provide the basic abstractions, attributes, methods, relationships, multiplicities, and navigabilities as appropriate.

"...” means much code is omitted.

This Java code is about Monsters in a video game.

Be sure to leave a note on the diagram highlighting how you address the violation

interface Monster {
    // move object to x, y
    public void translate(double x, double y);
    public void draw();
}
// Blob is an amphorous pile of goo
class Blob implements Monster {
    public void translate(double x, double y) { /* ... */ }
    public void draw() { /* ... */ }
    // ...
}
// ImmobileBlob is the blob that doesn't move!
class ImmobileBlob extends Blob {
    private int x;
    private int y;
    ImmobileBlob(int x, int y) { /* ... */ }
    public void translate(double x, double y) {
        // do nothing
    }
    // ...
}
class GameCanvas {
    // ...
    // translate the monsters into a 16 x 16 grid
    void gridify(Monster[] xs) {
        for (int i = 0; i < xs.length; i++) {
            xs[i].translate(i / 16, i % 16); // % is modulus
            xs[i].draw();
        }
    }
}