-
Notifications
You must be signed in to change notification settings - Fork 39
AnimalFactory #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
motabhai-bhagwaan
wants to merge
8
commits into
fengyuanyang:main
Choose a base branch
from
motabhai-bhagwaan:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
AnimalFactory #104
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
65a9a79
Added AnimalFactory to illustrate Animal Factory Design Pattern
itachi-2016 610a6c3
Made the required changes based on the last PR suggestions
itachi-2016 61be2fe
Delete the factory runner
itachi-2016 93f7025
Merge remote-tracking branch 'origin/main' into FactoryPattern#51
itachi-2016 8ab46c5
Merge pull request #1 from motabhai-bhagwaan/FactoryPattern#51
motabhai-bhagwaan f1b2cee
Added new tests and modified the AnimalFactory method so that it can …
itachi-2016 7580081
Added new tests and modified the AnimalFactory method so that it can …
itachi-2016 4d8971a
Merge branch 'FactoryPattern#51' into main
itachi-2016 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/com/ordestiny/tdd/designpattern/factorypattern/AnimalFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern; | ||
|
|
||
| import com.ordestiny.tdd.designpattern.factorypattern.animal.Animal; | ||
| import com.ordestiny.tdd.designpattern.factorypattern.animal.AnimalEnum; | ||
|
|
||
| import java.security.InvalidParameterException; | ||
| import java.text.MessageFormat; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
| public class AnimalFactory { | ||
| public static Animal getAnimal(String animal) { | ||
| AnimalEnum animalEnum = AnimalEnum.valueOf(animal); | ||
| if(animalEnum.getInstance() == null){ | ||
| throw new InvalidParameterException(); | ||
| } | ||
| return animalEnum.getInstance(); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/ordestiny/tdd/designpattern/factorypattern/animal/Animal.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern.animal; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
| public interface Animal { | ||
| int numberOfLegs(); | ||
| AnimalType typeOfAnimal(); | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/ordestiny/tdd/designpattern/factorypattern/animal/AnimalEnum.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern.animal; | ||
|
|
||
| import java.security.InvalidParameterException; | ||
| import java.text.MessageFormat; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
| public enum AnimalEnum { | ||
| DOG(Dog::new), HUMAN(Human::new), SNAKE(Snake::new), FROG(Frog::new), | ||
| EAGLE(Eagle::new), SALMON(Salmon::new), EARTHWORM(Earthworm::new); | ||
|
|
||
| private Supplier<Animal> initiator; | ||
|
|
||
| public Animal getInstance(){ | ||
| return initiator.get(); | ||
| } | ||
|
|
||
| AnimalEnum(Supplier<Animal> initiator){ | ||
| this.initiator = initiator; | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/ordestiny/tdd/designpattern/factorypattern/animal/AnimalType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern.animal; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
| public enum AnimalType { | ||
| MAMMAL, REPTILE, AMPHIBIAN, | ||
| BIRD, INVERTEBRATE, FISH; | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/ordestiny/tdd/designpattern/factorypattern/animal/Dog.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern.animal; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
| public class Dog implements Animal { | ||
|
|
||
| @Override | ||
| public int numberOfLegs() { | ||
| return 4; | ||
| } | ||
|
|
||
| @Override | ||
| public AnimalType typeOfAnimal() { | ||
| return AnimalType.MAMMAL; | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/ordestiny/tdd/designpattern/factorypattern/animal/Eagle.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern.animal; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
| public class Eagle implements Animal{ | ||
| @Override | ||
| public int numberOfLegs() { | ||
| return 2; | ||
| } | ||
|
|
||
| @Override | ||
| public AnimalType typeOfAnimal() { | ||
| return AnimalType.BIRD; | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/ordestiny/tdd/designpattern/factorypattern/animal/Earthworm.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern.animal; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
| public class Earthworm implements Animal{ | ||
| @Override | ||
| public int numberOfLegs() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public AnimalType typeOfAnimal() { | ||
| return AnimalType.INVERTEBRATE; | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/ordestiny/tdd/designpattern/factorypattern/animal/Frog.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern.animal; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
| public class Frog implements Animal { | ||
| @Override | ||
| public int numberOfLegs() { | ||
| return 2; | ||
| } | ||
|
|
||
| @Override | ||
| public AnimalType typeOfAnimal() { | ||
| return AnimalType.AMPHIBIAN; | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/ordestiny/tdd/designpattern/factorypattern/animal/Human.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern.animal; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
| public class Human implements Animal{ | ||
| @Override | ||
| public int numberOfLegs() { | ||
| return 2; | ||
| } | ||
|
|
||
| @Override | ||
| public AnimalType typeOfAnimal() { | ||
| return AnimalType.MAMMAL; | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/ordestiny/tdd/designpattern/factorypattern/animal/Salmon.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern.animal; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
| public class Salmon implements Animal { | ||
|
|
||
| @Override | ||
| public int numberOfLegs() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public AnimalType typeOfAnimal() { | ||
| return AnimalType.FISH; | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/ordestiny/tdd/designpattern/factorypattern/animal/Snake.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern.animal; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
| public class Snake implements Animal { | ||
|
|
||
| @Override | ||
| public int numberOfLegs() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public AnimalType typeOfAnimal() { | ||
| return AnimalType.REPTILE; | ||
| } | ||
| } |
95 changes: 95 additions & 0 deletions
95
src/test/java/com/ordestiny/tdd/designpattern/factorypattern/AnimalFactoryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package com.ordestiny.tdd.designpattern.factorypattern; | ||
|
|
||
| import com.ordestiny.tdd.designpattern.factorypattern.animal.*; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * @author Rachit Parikh | ||
| */ | ||
|
|
||
| public class AnimalFactoryTest { | ||
|
|
||
| Animal animal; | ||
|
|
||
| // CheckX test classes will check whether the factory method is able to create X | ||
| // It will also test if it working correctly, i.e whether the data returned by the class is correct or not | ||
|
|
||
| @Test | ||
| public void checkDog() throws Exception{ | ||
| animal = AnimalFactory.getAnimal("DOG"); | ||
| assertEquals(animal.getClass(), Dog.class); | ||
| assertEquals(animal.numberOfLegs(), 4); | ||
| assertEquals(animal.typeOfAnimal(), AnimalType.MAMMAL); | ||
| } | ||
|
|
||
| @Test | ||
| public void checkHuman() throws Exception{ | ||
| animal = AnimalFactory.getAnimal("HUMAN"); | ||
| assertEquals(animal.getClass(), Human.class); | ||
| assertEquals(animal.numberOfLegs(), 2); | ||
| assertEquals(animal.typeOfAnimal(), AnimalType.MAMMAL); | ||
| } | ||
|
|
||
| @Test | ||
| public void checkSalmon() throws Exception{ | ||
| animal = AnimalFactory.getAnimal("SALMON"); | ||
| assertEquals(animal.getClass(), Salmon.class); | ||
| assertEquals(animal.numberOfLegs(), 0); | ||
| assertEquals(animal.typeOfAnimal(), AnimalType.FISH); | ||
| } | ||
|
|
||
| @Test | ||
| public void checkSnake() throws Exception{ | ||
| animal = AnimalFactory.getAnimal("SNAKE"); | ||
| assertEquals(animal.getClass(), Snake.class); | ||
| assertEquals(animal.numberOfLegs(), 0); | ||
| assertEquals(animal.typeOfAnimal(), AnimalType.REPTILE); | ||
| } | ||
|
|
||
| @Test | ||
| public void checkEarthworm() throws Exception{ | ||
| animal = AnimalFactory.getAnimal("EARTHWORM"); | ||
| assertEquals(animal.getClass(), Earthworm.class); | ||
| assertEquals(animal.numberOfLegs(), 0); | ||
| assertEquals(animal.typeOfAnimal(), AnimalType.INVERTEBRATE); | ||
| } | ||
|
|
||
| @Test | ||
| public void checkEagle() throws Exception{ | ||
| animal = AnimalFactory.getAnimal("EAGLE"); | ||
| assertEquals(animal.getClass(), Eagle.class); | ||
| assertEquals(animal.numberOfLegs(), 2); | ||
| assertEquals(animal.typeOfAnimal(), AnimalType.BIRD); | ||
| } | ||
|
|
||
| @Test | ||
| public void checkFrog() throws Exception{ | ||
| animal = AnimalFactory.getAnimal("FROG"); | ||
| assertEquals(animal.getClass(), Frog.class); | ||
| assertEquals(animal.numberOfLegs(), 2); | ||
| assertEquals(animal.typeOfAnimal(), AnimalType.AMPHIBIAN); | ||
| } | ||
|
|
||
| @Test | ||
| public void checkBat() throws Exception{ | ||
| try { | ||
| animal = AnimalFactory.getAnimal("BAT"); | ||
| } | ||
| catch (Exception e) { | ||
| assertEquals(e.getClass(), IllegalArgumentException.class); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void checkTelevision() throws Exception{ | ||
| try{ | ||
| animal = AnimalFactory.getAnimal("TELEVISION"); | ||
| } | ||
| catch (Exception e){ | ||
| assertEquals(e.getClass(), IllegalArgumentException.class); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this block can be replaced with