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 [googleplusone]
If you know anyone interested in Object Oriented Design, feel free to share
Code From the Video
GetSilverPrice.java
public abstract class GetSilverPrice { abstract public double getPriceOfSilver(); public String getName() { return null; } }
ABCSilver.java
public class ABCSilver extends GetSilverPrice{ private String silverPrice = "SLV 30.66"; private String name = "ABC Silver"; public double getPriceOfSilver() { String stringPrice = silverPrice.substring(4); return Double.parseDouble(stringPrice); } public String getName(){ return name; } }
XYZSilver.java
public class XYZSilver extends GetSilverPrice{ private String silverPrice = "30.67 Silver Ask Price"; private String name = "XYZ Silver"; public double getPriceOfSilver() { String[] stringPrice = silverPrice.split(" "); return Double.parseDouble(stringPrice[0]); } public String getName(){ return name; } }
SilverPrices.java
import java.util.ArrayList; public class SilverPrices { public static void main(String[] args){ ArrayList<GetSilverPrice> silverSellers = new ArrayList<GetSilverPrice>(); silverSellers.add(new ABCSilver()); silverSellers.add(new XYZSilver()); for(GetSilverPrice silverPrice : silverSellers){ System.out.println(silverPrice.getName() + ": " + silverPrice.getPriceOfSilver()); } } }
EnemyAttacker.java
// 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(); } }
TestAdapter.java
public class TestAdapter { public static void main(String[] args){ EnemyAttacker theTank = new EnemyTank(); EnemyRobot theRobot = new EnemyRobot(); EnemyAttacker robotAdapter = new EnemyRobotAdapter(theRobot); theTank.attack(); robotAdapter.attack(); } }
Thank you so much for these tutorials. I am in the process of improving my skills and these videos just make everything easier to understand.
You’re very welcome 🙂 I’m very happy that they have helped
Thank you. These are awesome tutorial. You think you can do more of these?
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.