Skip to content

Commit

Permalink
Added ships example to factory method pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
luisburgos committed Jul 16, 2015
1 parent c13ef11 commit fe266f1
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/factory/examples/ships/EnemyShipFactory.java
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;
}
}
14 changes: 14 additions & 0 deletions src/factory/examples/ships/RocketShip.java
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);
}

}
44 changes: 44 additions & 0 deletions src/factory/examples/ships/Ship.java
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());
}
}
8 changes: 8 additions & 0 deletions src/factory/examples/ships/ShipFactory.java
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);
}
31 changes: 31 additions & 0 deletions src/factory/examples/ships/ShipTestDrive.java
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.");
}

}

}
14 changes: 14 additions & 0 deletions src/factory/examples/ships/UFOShip.java
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);
}

}
7 changes: 0 additions & 7 deletions src/factory/examples/weapons/Weapon.java

This file was deleted.

0 comments on commit fe266f1

Please sign in to comment.