In this tutorial I finish covering the GRASP patterns I covered previously here GRASP Tutorial.

This time talk about Polymorphism, Indirection and Protected Variations. They show how you can create flexibility in your system in different ways by having different classes implement interfaces. I also talk about Pure Fabrication, which shows the importance of separating out code that impedes low coupling. Code follows the video to help you.

If you like videos like this, it helps to tell Google

If you know anyone interested in Object Oriented Design, feel free to share

// The adapter will make new classes
// compatible with this one
public interface EnemyAttacker {
public void attack();
}

EnemyTank.java

// EnemyTank has the attack method so it is easy to
// work with
public class EnemyTank implements EnemyAttacker{
public void attack() {
System.out.println("Tank fires 2 missiles");
}
}

EnemyRobot.java

// This class doesn't have the attack method
public class EnemyRobot {
public void jumpOnEnemy(){
System.out.println("Robot jumps on the enemy");
}
}

EnemyRobotAdapter.java

// The adapter calls the method jumpOnEnemy when
// an EnemyAttacker is supposed to call attack
// which is defined in the interface
public class EnemyRobotAdapter implements EnemyAttacker{
EnemyRobot theRobot;
public EnemyRobotAdapter(EnemyRobot newRobot){
theRobot = newRobot;
}
// When this method is called call the correct
// method for the EnemyRobot
public void attack() {
theRobot.jumpOnEnemy();
}
}

Thank you 🙂 When I start making Android Games I’m going to model them out like this first and then make the games from the models. I didn’t do that in the past because I can never trust that people saw the Object Oriented Design tutorials first. That is one of the flaws with online education. I can’t control which videos people watch first.