fix abstract factory ex

This commit is contained in:
Loredana 2019-01-02 21:58:02 +02:00
parent b078d0fb3d
commit 9a8e006d73
5 changed files with 10 additions and 21 deletions

View File

@ -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) ;
}

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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);