fix abstract factory ex
This commit is contained in:
parent
b078d0fb3d
commit
9a8e006d73
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public interface AbstractFactory {
|
||||
Animal getAnimal(String toyType) ;
|
||||
Color getColor(String colorType);
|
||||
public interface AbstractFactory<T> {
|
||||
T create(String type) ;
|
||||
}
|
|
@ -6,10 +6,10 @@ public class AbstractPatternDriver {
|
|||
|
||||
//creating a brown toy dog
|
||||
abstractFactory = FactoryProvider.getFactory("Toy");
|
||||
Animal toy = abstractFactory.getAnimal("Dog");
|
||||
Animal toy =(Animal) abstractFactory.create("Dog");
|
||||
|
||||
abstractFactory = FactoryProvider.getFactory("Color");
|
||||
Color color = abstractFactory.getColor("Brown");
|
||||
Color color =(Color) abstractFactory.create("Brown");
|
||||
|
||||
String result = "A " + toy.getType() + " with " + color.getColor() + " color " + toy.makeSound();
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public class AnimalFactory implements AbstractFactory {
|
||||
public class AnimalFactory implements AbstractFactory<Animal> {
|
||||
|
||||
@Override
|
||||
public Animal getAnimal(String animalType) {
|
||||
public Animal create(String animalType) {
|
||||
if ("Dog".equalsIgnoreCase(animalType)) {
|
||||
return new Dog();
|
||||
} else if ("Duck".equalsIgnoreCase(animalType)) {
|
||||
|
@ -13,9 +13,4 @@ public class AnimalFactory implements AbstractFactory {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getColor(String color) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
public class ColorFactory implements AbstractFactory {
|
||||
public class ColorFactory implements AbstractFactory<Color> {
|
||||
|
||||
@Override
|
||||
public Color getColor(String colorType) {
|
||||
public Color create(String colorType) {
|
||||
if ("Brown".equalsIgnoreCase(colorType)) {
|
||||
return new Brown();
|
||||
} else if ("White".equalsIgnoreCase(colorType)) {
|
||||
|
@ -13,9 +13,4 @@ public class ColorFactory implements AbstractFactory {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Animal getAnimal(String toyType) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,10 +11,10 @@ public class AbstractPatternIntegrationTest {
|
|||
|
||||
//creating a brown toy dog
|
||||
abstractFactory = FactoryProvider.getFactory("Toy");
|
||||
Animal toy = abstractFactory.getAnimal("Dog");
|
||||
Animal toy = (Animal) abstractFactory.create("Dog");
|
||||
|
||||
abstractFactory = FactoryProvider.getFactory("Color");
|
||||
Color color = abstractFactory.getColor("Brown");
|
||||
Color color =(Color) abstractFactory.create("Brown");
|
||||
|
||||
String result = "A " + toy.getType() + " with " + color.getColor() + " color " + toy.makeSound();
|
||||
assertEquals("A Dog with brown color Barks", result);
|
||||
|
|
Loading…
Reference in New Issue