2) Program To An Interface Not To An Implementation 3) Identity The Aspects of Your Application That Vary and Separate Them From What Stays The Same
2) Program To An Interface Not To An Implementation 3) Identity The Aspects of Your Application That Vary and Separate Them From What Stays The Same
Explanation 2:
http://www.newthinktank.com/2012/08/strategy-design-pattern-tutorial/
Animal.Java
public class Animal {
private String name;
private double height;
private int weight;
Dog.java
public class Dog extends Animal{
public void digHole(){
System.out.println("Dug a hole");
}
public Dog(){
super();
setSound("Bark");
// We set the Flys interface polymorphically
// This sets the behavior as a non-flying Animal
flyingType = new CantFly();
}
/* BAD
* You could override the fly method, but we are breaking
* the rule that we need to abstract what is different to
* the subclasses
*
public void fly(){
System.out.println("I can't fly");
}
*/
}
BIRD.JAVA
public class Bird extends Animal{
// The constructor initializes all objects
public Bird(){
super();
setSound("Tweet");
// We set the Flys interface polymorphically
// This sets the behavior as a non-flying Animal
flyingType = new ItFlys();
}
}
FLYS.JAVA
// The interface is implemented by many other