Skip to content

Instantly share code, notes, and snippets.

@wbashir
Created May 10, 2014 03:09
Show Gist options
  • Save wbashir/4ac27fc562f0d3ca8002 to your computer and use it in GitHub Desktop.
Save wbashir/4ac27fc562f0d3ca8002 to your computer and use it in GitHub Desktop.
HW5
/*Main.java*/
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog = new Name(dog, "Fido");
dog = new Color(dog, "brown");
Animal cat = new Cat();
cat = new Name(cat, "Felix");
cat = new Color(cat, "white");
Animal fish = new Fish();
fish = new Name(fish, "Nemo");
fish = new Color(fish, "orange");
Animal worm = new Worm();
worm = new Color(worm, "grey");
Animal cricket = new Cricket();
cricket = new Color(cricket, "black");
Animal[] animals = { dog, cat, fish, worm, cricket };
for (Animal animal : animals)
System.out.println(animal.getDescription());
}
}
/*Color*/
public class Color extends AnimalDecorator {
private Animal animal;
private String color;
public Color(Animal animal, String color) {
this.animal = animal;
this.color = color;
}
public String getColor(){return color;}
public String getDescription() {
return animal.getDescription() + " color=" + color;
}
}
/*Cricket*/
public class Cricket extends Animal {
public Cricket() {
description = "Cricket:: sound=chirp";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment