Skip to content

Commit bd984f1

Browse files
authored
Create PetFactory.java
1 parent d0ad063 commit bd984f1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

patterns/factory/PetFactory.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.zetcode;
2+
3+
public class PetFactory {
4+
5+
public static Pet create(Class clazz, String name, String type) {
6+
7+
if (clazz.equals(Cat.class)) {
8+
9+
return new Cat(name, type);
10+
}
11+
12+
if (clazz.equals(Dog.class)) {
13+
14+
return new Dog(name, type);
15+
}
16+
17+
if (clazz.equals(Parrot.class)) {
18+
19+
return new Parrot(name, type);
20+
}
21+
22+
throw new IllegalArgumentException("Invalid Pet");
23+
}
24+
}

0 commit comments

Comments
 (0)