This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Duck implements Animal { | |
@Override | |
public String makeNoise() { | |
return "Quack"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AnimalFactory { | |
public Animal getAnimal(String animal) { | |
if (animal == null) | |
return null; | |
else if (animal.equalsIgnoreCase(Dog.class.getSimpleName())) | |
return new Dog(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainWithRuntimeInput { | |
public static void main(String[] args) { | |
Scanner reader = new Scanner(System.in); | |
System.out.print("Enter an Animal: "); | |
String animalName = reader.next(); | |
reader.close(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
//Create instance of Factory | |
AnimalFactory animalFactory = new AnimalFactory(); | |
//Obtain Dog object from the factory | |
Animal dog = animalFactory.getAnimal("Dog"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Shape { | |
void draw(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Circle implements Shape { | |
@Override | |
public void draw() { | |
System.out.println("Drawing a Circle"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Rectangle implements Shape { | |
@Override | |
public void draw() { | |
System.out.println("Drawing a Rectangle"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Square implements Shape { | |
@Override | |
public void draw() { | |
System.out.println("Drawing a Square"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Color { | |
void paint(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Blue implements Color { | |
@Override | |
public void paint() { | |
System.out.println("Painting color Blue"); | |
} | |
} |