Skip to content

Commit 9a8e006

Browse files
committed
fix abstract factory ex
1 parent b078d0f commit 9a8e006

File tree

5 files changed

+10
-21
lines changed

5 files changed

+10
-21
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.creational.abstractfactory;
22

3-
public interface AbstractFactory {
4-
Animal getAnimal(String toyType) ;
5-
Color getColor(String colorType);
3+
public interface AbstractFactory<T> {
4+
T create(String type) ;
65
}

patterns/design-patterns/src/main/java/com/baeldung/creational/abstractfactory/AbstractPatternDriver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ public static void main(String[] args) {
66

77
//creating a brown toy dog
88
abstractFactory = FactoryProvider.getFactory("Toy");
9-
Animal toy = abstractFactory.getAnimal("Dog");
9+
Animal toy =(Animal) abstractFactory.create("Dog");
1010

1111
abstractFactory = FactoryProvider.getFactory("Color");
12-
Color color = abstractFactory.getColor("Brown");
12+
Color color =(Color) abstractFactory.create("Brown");
1313

1414
String result = "A " + toy.getType() + " with " + color.getColor() + " color " + toy.makeSound();
1515

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.baeldung.creational.abstractfactory;
22

3-
public class AnimalFactory implements AbstractFactory {
3+
public class AnimalFactory implements AbstractFactory<Animal> {
44

55
@Override
6-
public Animal getAnimal(String animalType) {
6+
public Animal create(String animalType) {
77
if ("Dog".equalsIgnoreCase(animalType)) {
88
return new Dog();
99
} else if ("Duck".equalsIgnoreCase(animalType)) {
@@ -13,9 +13,4 @@ public Animal getAnimal(String animalType) {
1313
return null;
1414
}
1515

16-
@Override
17-
public Color getColor(String color) {
18-
throw new UnsupportedOperationException();
19-
}
20-
2116
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.baeldung.creational.abstractfactory;
22

3-
public class ColorFactory implements AbstractFactory {
3+
public class ColorFactory implements AbstractFactory<Color> {
44

55
@Override
6-
public Color getColor(String colorType) {
6+
public Color create(String colorType) {
77
if ("Brown".equalsIgnoreCase(colorType)) {
88
return new Brown();
99
} else if ("White".equalsIgnoreCase(colorType)) {
@@ -13,9 +13,4 @@ public Color getColor(String colorType) {
1313
return null;
1414
}
1515

16-
@Override
17-
public Animal getAnimal(String toyType) {
18-
throw new UnsupportedOperationException();
19-
}
20-
2116
}

patterns/design-patterns/src/test/java/com/baeldung/creational/abstractfactory/AbstractPatternIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public void givenAbstractFactory_whenGettingObjects_thenSuccessful() {
1111

1212
//creating a brown toy dog
1313
abstractFactory = FactoryProvider.getFactory("Toy");
14-
Animal toy = abstractFactory.getAnimal("Dog");
14+
Animal toy = (Animal) abstractFactory.create("Dog");
1515

1616
abstractFactory = FactoryProvider.getFactory("Color");
17-
Color color = abstractFactory.getColor("Brown");
17+
Color color =(Color) abstractFactory.create("Brown");
1818

1919
String result = "A " + toy.getType() + " with " + color.getColor() + " color " + toy.makeSound();
2020
assertEquals("A Dog with brown color Barks", result);

0 commit comments

Comments
 (0)