Last active
August 29, 2015 14:02
-
-
Save kaznishi/0a79743d64fb4ed484b9 to your computer and use it in GitHub Desktop.
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
<?php | |
/* | |
class一覧 | |
Human系 | |
└Human(Abstract) | |
└Japanese 日本人クラス。cm,kgで身長体重を入出力 | |
└American 米国人クラス。ft,lbで身長体重を入出力 | |
└English 英国人クラス。ft,stで身長体重を入出力 | |
└Hyde ハイドクラス。hyde,kgで身長体重を入出力 | |
Converter系 | |
└AbstractHumanConverter(Abstract) | |
└JapaneseConverter 入力したHuman派生のオブジェクトを日本人オブジェクトに変換 | |
└AmericanConverter 入力したHuman派生のオブジェクトを米国人オブジェクトに変換 | |
└EnglishConverter 入力したHuman派生のオブジェクトを英国人オブジェクトに変換 | |
└HydeConverter 入力したHuman派生のオブジェクトをハイドオブジェクトに変換 | |
*/ | |
// グローバル定数 | |
define('hydePerCm', 0.00641026); // 1hyde = 156cm | |
define('ftPerCm', 0.0328084); // 1feet = 30.48cm | |
define('inPerCm', 0.39370079); // 1inch = 2.54cm | |
define('lbPerKg', 2.20462262); // 1ポンド = 0.45359237kg | |
define('stPerKg', 0.15747304); // 1ストーン = 6.35029318kg | |
// 人クラス群 | |
abstract class Human | |
{ | |
private $weight; //kg | |
private $height; //cm | |
public function __construct($height, $weight) | |
{ | |
$this->setHeight($height); | |
$this->setWeight($weight); | |
} | |
abstract public function getHeightRate(); | |
abstract public function getWeightRate(); | |
public function getHeight() | |
{ | |
return $this->height * $this->getHeightRate(); | |
} | |
public function getHeightCm() | |
{ | |
return $this->height; | |
} | |
public function setHeight($height) | |
{ | |
$this->height = (float)$height / $this->getHeightRate(); | |
} | |
public function setHeightCm($height) | |
{ | |
$this->height = (float)$height; | |
} | |
public function getWeight() | |
{ | |
return $this->weight * $this->getWeightRate(); | |
} | |
public function getWeightKg() | |
{ | |
return $this->weight; | |
} | |
public function setWeight($weight) | |
{ | |
$this->weight = (float)$weight / $this->getWeightRate(); | |
} | |
public function setWeightKg($weight) | |
{ | |
$this->weight = (float)$weight; | |
} | |
} | |
class Japanese extends Human | |
{ | |
public function getHeightRate() | |
{ | |
return 1; | |
} | |
public function getWeightRate() | |
{ | |
return 1; | |
} | |
} | |
class American extends Human | |
{ | |
public function getHeightRate() | |
{ | |
return ftPerCm; | |
} | |
public function getWeightRate() | |
{ | |
return lbPerKg; | |
} | |
} | |
class English extends Human | |
{ | |
public function getHeightRate() | |
{ | |
return ftPerCm; | |
} | |
public function getWeightRate() | |
{ | |
return stPerKg; | |
} | |
} | |
class Hyde extends Human | |
{ | |
public function getHeightRate() | |
{ | |
return hydePerCm; | |
} | |
public function getWeightRate() | |
{ | |
return 1; | |
} | |
} | |
// 人変換クラス群 | |
abstract class AbstractHumanConverter | |
{ | |
abstract public function getNewHuman(); | |
public function convert(Human $human) | |
{ | |
$newHuman = $this->getNewHuman(); | |
$newHuman->setHeightCm($human->getHeightCm()); | |
$newHuman->setWeightKg($human->getWeightKg()); | |
return $newHuman; | |
} | |
} | |
class JapaneseConverter extends AbstractHumanConverter | |
{ | |
public function getNewHuman() | |
{ | |
return new Japanese(null,null); | |
} | |
} | |
class AmericanConverter extends AbstractHumanConverter | |
{ | |
public function getNewHuman() | |
{ | |
return new American(null,null); | |
} | |
} | |
class EnglishConverter extends AbstractHumanConverter | |
{ | |
public function getNewHuman() | |
{ | |
return new English(null,null); | |
} | |
} | |
class HydeConverter extends AbstractHumanConverter | |
{ | |
public function getNewHuman() | |
{ | |
return new Hyde(null,null); | |
} | |
} | |
/////////////////////////////////////////////////////////// | |
// 身長168.2cm, 65.0kgのほげ谷さん | |
echo "HOGETANI\n"; | |
echo "//////// Japanese /////////\n"; | |
$hogetani = new Japanese(168.2, 65.0); | |
echo "Japanese height : {$hogetani->getHeight()} [cm]\n"; | |
echo "Japanese weight : {$hogetani->getWeight()} [kg]\n"; | |
// アメリカ単位系への変換 | |
echo "//////// American /////////\n"; | |
$converter = new AmericanConverter(); | |
$hogetani_as_american = $converter->convert($hogetani); | |
echo "American height : {$hogetani_as_american->getHeight()} [ft]\n"; | |
echo "American weight : {$hogetani_as_american->getWeight()} [lb]\n"; | |
// Hyde単位系への変換 | |
echo "//////// Hyde /////////\n"; | |
$converter = new HydeConverter(); | |
$hogetani_as_hyde = $converter->convert($hogetani); | |
echo "Hyde height : {$hogetani_as_hyde->getHeight()} [hyde]\n"; | |
echo "Hyde weight : {$hogetani_as_hyde->getWeight()} [kg]\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment