Skip to content

Instantly share code, notes, and snippets.

@xwlee
Created February 23, 2013 10:40
Show Gist options
  • Save xwlee/a17c94c0e490ab971a55 to your computer and use it in GitHub Desktop.
Save xwlee/a17c94c0e490ab971a55 to your computer and use it in GitHub Desktop.
<?php
abstract class enemyShip {
protected $name;
protected $amtDamage;
public function setName($newName) {
$this->name = $newName;
}
public function getName() {
return $this->name;
}
public function setDamage($newDamage) {
$this->amtDamage = $newDamage;
}
public function getDamage() {
return $this->amtDamage;
}
public function followHeroShip() {
echo $this->getName() . " is following the hero <br/>";
}
public function displayEnemyShip() {
echo $this->getName() . " is on the screen <br />";
}
public function enemyShipShoots() {
echo $this->getName() . " attack and does " . $this->getDamage() . "<br />";
}
}
class UFOEnemyShip extends enemyShip {
public function __construct() {
parent::setName("UFO Enemy Ship");
parent::setDamage(20);
}
}
class RocketEnemyShip extends enemyShip {
public function __construct() {
parent::setName("Rocket Enemy Ship");
parent::setDamage(10);
}
}
// Before using Factory Design Pattern
class enemyShipTesting {
public function __construct() {
/**
* Bad stuff
* It doesn't close the code for being modified (Open Close Principle)
* Tons of if else statement
**/
if (isset($_POST['type'])) {
if ($_POST['type'] == "u") {
$ufoShip = new UFOEnemyShip();
} elseif ($_POST['type'] == "r") {
$ufoShip = new RocketEnemyShip();
}
$this->doStuffEnemy($ufoShip);
}
echo '<form method="post">',
'<label for="type">Type: </label>',
'<input type"text" id="type" name="type" />',
'</form>';
}
public function doStuffEnemy(enemyShip $anEnemyShip) {
$anEnemyShip->followHeroShip();
$anEnemyShip->displayEnemyShip();
$anEnemyShip->enemyShipShoots();
}
}
$testing = new enemyShipTesting();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment