forked from luisburgos/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ships example to factory method pattern.
- Loading branch information
1 parent
c13ef11
commit fe266f1
Showing
7 changed files
with
130 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,19 @@ | ||
package factory.examples.ships; | ||
|
||
/** | ||
* Created by luisburgos on 16/07/15. | ||
*/ | ||
public class EnemyShipFactory implements ShipFactory{ | ||
@Override | ||
public Ship createShip(String shipType) { | ||
Ship ship = null; | ||
|
||
if(shipType.equalsIgnoreCase("rocket")){ | ||
ship = new RocketShip(); | ||
}else if(shipType.equalsIgnoreCase("ufo")){ | ||
ship = new UFOShip(); | ||
} | ||
|
||
return ship; | ||
} | ||
} |
This file contains 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,14 @@ | ||
package factory.examples.ships; | ||
|
||
/** | ||
* Created by luisburgos on 16/07/15. | ||
*/ | ||
public class RocketShip extends Ship { | ||
|
||
public RocketShip (){ | ||
setName("Rocket Ship"); | ||
setDamage(10); | ||
setSpeed(1000); | ||
} | ||
|
||
} |
This file contains 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,44 @@ | ||
package factory.examples.ships; | ||
|
||
/** | ||
* Created by luisburgos on 16/07/15. | ||
*/ | ||
public abstract class Ship { | ||
|
||
private String name; | ||
private double speed; | ||
private double damage; | ||
|
||
public Ship() { | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
|
||
this.name = name; | ||
} | ||
|
||
public double getSpeed() { | ||
return speed; | ||
} | ||
|
||
public void setSpeed(double speed) { | ||
this.speed = speed; | ||
} | ||
|
||
public double getDamage() { | ||
return damage; | ||
} | ||
|
||
public void setDamage(double damage) { | ||
this.damage = damage; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Name: " + getName() + "| Damage: " + String.format("%.2f", getDamage()) + "| Speed:" + String.format("%.2f", getSpeed()); | ||
} | ||
} |
This file contains 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,8 @@ | ||
package factory.examples.ships; | ||
|
||
/** | ||
* Created by luisburgos on 15/07/15. | ||
*/ | ||
public interface ShipFactory { | ||
public Ship createShip(String shipType); | ||
} |
This file contains 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,31 @@ | ||
package factory.examples.ships; | ||
|
||
/** | ||
* Created by luisburgos on 16/07/15. | ||
*/ | ||
public class ShipTestDrive { | ||
|
||
public static void main(String[] args) { | ||
|
||
ShipFactory shipFactory = new EnemyShipFactory(); | ||
Ship shipCreated; | ||
|
||
System.out.println("ROCKET: "); | ||
shipCreated = shipFactory.createShip("rocket"); | ||
if(shipCreated!= null){ | ||
System.out.println(shipCreated.toString()); | ||
}else{ | ||
System.out.println("No ship created."); | ||
} | ||
|
||
System.out.println("UFO: "); | ||
shipCreated = shipFactory.createShip("UFO"); | ||
if(shipCreated!= null){ | ||
System.out.println(shipCreated.toString()); | ||
}else{ | ||
System.out.println("No ship created."); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains 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,14 @@ | ||
package factory.examples.ships; | ||
|
||
/** | ||
* Created by luisburgos on 16/07/15. | ||
*/ | ||
public class UFOShip extends Ship { | ||
|
||
public UFOShip (){ | ||
setName("UFO Ship"); | ||
setDamage(30); | ||
setSpeed(3000); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.