Merge pull request #6054 from eugenp/fix-abstract-factory

fix abstract factory ex
This commit is contained in:
Loredana Crusoveanu 2019-01-02 22:13:30 +02:00 committed by GitHub
commit 0227e51689
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 21 deletions

View File

@ -1,6 +1,5 @@
package com.baeldung.creational.abstractfactory; package com.baeldung.creational.abstractfactory;
public interface AbstractFactory { public interface AbstractFactory<T> {
Animal getAnimal(String toyType) ; T create(String type) ;
Color getColor(String colorType);
} }

View File

@ -6,10 +6,10 @@ public class AbstractPatternDriver {
//creating a brown toy dog //creating a brown toy dog
abstractFactory = FactoryProvider.getFactory("Toy"); abstractFactory = FactoryProvider.getFactory("Toy");
Animal toy = abstractFactory.getAnimal("Dog"); Animal toy =(Animal) abstractFactory.create("Dog");
abstractFactory = FactoryProvider.getFactory("Color"); 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(); String result = "A " + toy.getType() + " with " + color.getColor() + " color " + toy.makeSound();

View File

@ -1,9 +1,9 @@
package com.baeldung.creational.abstractfactory; package com.baeldung.creational.abstractfactory;
public class AnimalFactory implements AbstractFactory { public class AnimalFactory implements AbstractFactory<Animal> {
@Override @Override
public Animal getAnimal(String animalType) { public Animal create(String animalType) {
if ("Dog".equalsIgnoreCase(animalType)) { if ("Dog".equalsIgnoreCase(animalType)) {
return new Dog(); return new Dog();
} else if ("Duck".equalsIgnoreCase(animalType)) { } else if ("Duck".equalsIgnoreCase(animalType)) {
@ -13,9 +13,4 @@ public class AnimalFactory implements AbstractFactory {
return null; return null;
} }
@Override
public Color getColor(String color) {
throw new UnsupportedOperationException();
}
} }

View File

@ -1,9 +1,9 @@
package com.baeldung.creational.abstractfactory; package com.baeldung.creational.abstractfactory;
public class ColorFactory implements AbstractFactory { public class ColorFactory implements AbstractFactory<Color> {
@Override @Override
public Color getColor(String colorType) { public Color create(String colorType) {
if ("Brown".equalsIgnoreCase(colorType)) { if ("Brown".equalsIgnoreCase(colorType)) {
return new Brown(); return new Brown();
} else if ("White".equalsIgnoreCase(colorType)) { } else if ("White".equalsIgnoreCase(colorType)) {
@ -13,9 +13,4 @@ public class ColorFactory implements AbstractFactory {
return null; 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 //creating a brown toy dog
abstractFactory = FactoryProvider.getFactory("Toy"); abstractFactory = FactoryProvider.getFactory("Toy");
Animal toy = abstractFactory.getAnimal("Dog"); Animal toy = (Animal) abstractFactory.create("Dog");
abstractFactory = FactoryProvider.getFactory("Color"); 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(); String result = "A " + toy.getType() + " with " + color.getColor() + " color " + toy.makeSound();
assertEquals("A Dog with brown color Barks", result); assertEquals("A Dog with brown color Barks", result);