File tree Expand file tree Collapse file tree 13 files changed +110
-46
lines changed
CreationalDesignPattern/SimpleFactoryPattern/Java/SimpleFactoryDemo
Creational/SimpleFactoryPattern/src/io/csie/chris Expand file tree Collapse file tree 13 files changed +110
-46
lines changed Original file line number Diff line number Diff line change 1+ package io .csie .chris ;
2+
3+ import io .csie .chris .animal .Animal ;
4+ import io .csie .chris .animal .common .AnimalType ;
5+ import io .csie .chris .factory .AnimalFactory ;
6+
7+ public class Main {
8+
9+ public static void main (String args []) {
10+
11+ Animal animal1 = AnimalFactory .createAnimal (AnimalType .Dog .getName ());
12+ System .out .println (animal1 .makeSound ());
13+
14+ Animal animal2 = AnimalFactory .createAnimal (AnimalType .Cat .getName ());
15+ System .out .println (animal2 .makeSound ());
16+
17+ Animal animal3 = AnimalFactory .createAnimal (AnimalType .Fox .getName ());
18+ System .out .println (animal3 .makeSound ());
19+
20+ Animal animal4 = AnimalFactory .createAnimal (AnimalType .Monster .getName ());
21+ System .out .println (animal4 .makeSound ());
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package io .csie .chris .animal ;
2+
13public abstract class Animal {
4+
25 public abstract String makeSound ();
36}
Original file line number Diff line number Diff line change 1+ package io .csie .chris .animal ;
2+
3+ import io .csie .chris .animal .common .AnimalType ;
4+
5+ public class Cat extends Animal {
6+
7+ @ Override
8+ public String makeSound () {
9+ return AnimalType .Cat .getSound ();
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ package io .csie .chris .animal ;
2+
3+ import io .csie .chris .animal .common .AnimalType ;
4+
5+ public class Dog extends Animal {
6+
7+ @ Override
8+ public String makeSound () {
9+ return AnimalType .Dog .getSound ();
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ package io .csie .chris .animal ;
2+
3+ import io .csie .chris .animal .common .AnimalType ;
4+
5+ public class Fox extends Animal {
6+
7+ @ Override
8+ public String makeSound () {
9+ return AnimalType .Fox .getSound ();
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ package io .csie .chris .animal ;
2+
3+ import io .csie .chris .animal .common .AnimalType ;
4+
5+ public class Monster extends Animal {
6+
7+ @ Override
8+ public String makeSound () {
9+ return AnimalType .Monster .getSound ();
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ package io .csie .chris .animal .common ;
2+
3+ public enum AnimalType {
4+
5+ Cat ("feline" , "Meow" ),
6+
7+ Dog ("canine" , "Bark" ),
8+
9+ Fox ("vulpine" , "Ssbb" ),
10+
11+ Monster ("xxxxxxx" , "Unknown" );
12+
13+ private String sound ;
14+
15+ private String name ;
16+
17+ AnimalType (String name , String sound ) {
18+ this .name = name ;
19+ this .sound = sound ;
20+ }
21+
22+ public String getSound () {
23+ return this .sound ;
24+ }
25+
26+ public String getName () {
27+ return this .name ;
28+ }
29+ }
Original file line number Diff line number Diff line change 1- import com .sun .istack .internal .Nullable ;
1+ package io .csie .chris .factory ;
2+
3+ import io .csie .chris .animal .*;
4+ import io .csie .chris .animal .common .AnimalType ;
25
36public class AnimalFactory {
7+
48 // Create the instance by parameter, and it's a static method
59 public static Animal createAnimal (String animalType ) {
6- Animal animal = null ;
710
8- if (animalType .equals ("canine" )) {
11+ Animal animal ;
12+
13+ if (animalType .equals (AnimalType .Dog .getName ())) {
914 animal = new Dog ();
10- } else if (animalType .equals ("feline" )) {
15+ } else if (animalType .equals (AnimalType . Cat . getName () )) {
1116 animal = new Cat ();
12- } else if (animalType .equals ("vulpine" )) {
17+ } else if (animalType .equals (AnimalType . Fox . getName () )) {
1318 animal = new Fox ();
1419 } else {
1520 animal = new Monster ();
1621 }
22+
1723 return animal ;
1824 }
1925}
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments