File tree 5 files changed +10
-21
lines changed
patterns/design-patterns/src
main/java/com/baeldung/creational/abstractfactory
test/java/com/baeldung/creational/abstractfactory
5 files changed +10
-21
lines changed Original file line number Diff line number Diff line change 1
1
package com .baeldung .creational .abstractfactory ;
2
2
3
- public interface AbstractFactory {
4
- Animal getAnimal (String toyType ) ;
5
- Color getColor (String colorType );
3
+ public interface AbstractFactory <T > {
4
+ T create (String type ) ;
6
5
}
Original file line number Diff line number Diff line change @@ -6,10 +6,10 @@ public static void main(String[] args) {
6
6
7
7
//creating a brown toy dog
8
8
abstractFactory = FactoryProvider .getFactory ("Toy" );
9
- Animal toy = abstractFactory .getAnimal ("Dog" );
9
+ Animal toy =( Animal ) abstractFactory .create ("Dog" );
10
10
11
11
abstractFactory = FactoryProvider .getFactory ("Color" );
12
- Color color = abstractFactory .getColor ("Brown" );
12
+ Color color =( Color ) abstractFactory .create ("Brown" );
13
13
14
14
String result = "A " + toy .getType () + " with " + color .getColor () + " color " + toy .makeSound ();
15
15
Original file line number Diff line number Diff line change 1
1
package com .baeldung .creational .abstractfactory ;
2
2
3
- public class AnimalFactory implements AbstractFactory {
3
+ public class AnimalFactory implements AbstractFactory < Animal > {
4
4
5
5
@ Override
6
- public Animal getAnimal (String animalType ) {
6
+ public Animal create (String animalType ) {
7
7
if ("Dog" .equalsIgnoreCase (animalType )) {
8
8
return new Dog ();
9
9
} else if ("Duck" .equalsIgnoreCase (animalType )) {
@@ -13,9 +13,4 @@ public Animal getAnimal(String animalType) {
13
13
return null ;
14
14
}
15
15
16
- @ Override
17
- public Color getColor (String color ) {
18
- throw new UnsupportedOperationException ();
19
- }
20
-
21
16
}
Original file line number Diff line number Diff line change 1
1
package com .baeldung .creational .abstractfactory ;
2
2
3
- public class ColorFactory implements AbstractFactory {
3
+ public class ColorFactory implements AbstractFactory < Color > {
4
4
5
5
@ Override
6
- public Color getColor (String colorType ) {
6
+ public Color create (String colorType ) {
7
7
if ("Brown" .equalsIgnoreCase (colorType )) {
8
8
return new Brown ();
9
9
} else if ("White" .equalsIgnoreCase (colorType )) {
@@ -13,9 +13,4 @@ public Color getColor(String colorType) {
13
13
return null ;
14
14
}
15
15
16
- @ Override
17
- public Animal getAnimal (String toyType ) {
18
- throw new UnsupportedOperationException ();
19
- }
20
-
21
16
}
Original file line number Diff line number Diff line change @@ -11,10 +11,10 @@ public void givenAbstractFactory_whenGettingObjects_thenSuccessful() {
11
11
12
12
//creating a brown toy dog
13
13
abstractFactory = FactoryProvider .getFactory ("Toy" );
14
- Animal toy = abstractFactory .getAnimal ("Dog" );
14
+ Animal toy = ( Animal ) abstractFactory .create ("Dog" );
15
15
16
16
abstractFactory = FactoryProvider .getFactory ("Color" );
17
- Color color = abstractFactory .getColor ("Brown" );
17
+ Color color =( Color ) abstractFactory .create ("Brown" );
18
18
19
19
String result = "A " + toy .getType () + " with " + color .getColor () + " color " + toy .makeSound ();
20
20
assertEquals ("A Dog with brown color Barks" , result );
You can’t perform that action at this time.
0 commit comments