diff --git a/PrinciplesSystemDesign/README.md b/PrinciplesSystemDesign/README.md new file mode 100644 index 0000000..77c31de --- /dev/null +++ b/PrinciplesSystemDesign/README.md @@ -0,0 +1,9 @@ +### 現場で役立つシステム設計の原則 + +本を読むとちょくちょくサンプルコードがJavaで紹介されて出てきます。 +これをPHPで写経して体現的に内容を学ぶ事を目的にしています。 + +### 参考資料 + +- [現場で役立つシステム設計の原則 - Fendo181](https://scrapbox.io/fendo181/%E7%8F%BE%E5%A0%B4%E3%81%A7%E5%BD%B9%E7%AB%8B%E3%81%A4%E3%82%B7%E3%82%B9%E3%83%86%E3%83%A0%E8%A8%AD%E8%A8%88%E3%81%AE%E5%8E%9F%E5%89%87) + diff --git a/PrinciplesSystemDesign/chap1/Quantity.php b/PrinciplesSystemDesign/chap1/Quantity.php new file mode 100644 index 0000000..5f77384 --- /dev/null +++ b/PrinciplesSystemDesign/chap1/Quantity.php @@ -0,0 +1,56 @@ + self::MAX) { + throw new Exception('100以上です'); + } + }catch (Exception $e ) { + echo '捕捉した例外: ', $e->getMessage(), "\n"; + } + + $this->value = $value; + } + + private function addValue($other) { + $sum = $this->value + $other; + return $sum; + } + + + public function canAdd(int $other) :bool { + $added = self::addValue($other); + return $added <= self::MAX; + } + + public function add(int $other) { + + try{ + if(!$this->canAdd($other)) { + throw new Exception('合計が100を超えています'); + } + $added = $this->addValue($other); + // addすると新しいオブジェクトを呼び出す + return new Quantity($added); + + }catch (Exception $e){ + echo '捕捉した例外: ', $e->getMessage(), "\n"; + } + } +} + +// これで一回新しいオブジェクトを生成する +$quanty = new Quantity(10); +echo $quanty->value; + +// 新しいオブジェクトを生成する +$quanty->add(100); \ No newline at end of file diff --git a/PrinciplesSystemDesign/chap1/ShippingCost.php b/PrinciplesSystemDesign/chap1/ShippingCost.php new file mode 100644 index 0000000..a281b04 --- /dev/null +++ b/PrinciplesSystemDesign/chap1/ShippingCost.php @@ -0,0 +1,30 @@ +basePrice = $basePrice; + } + + public function amount () :int{ + if( $this->basePrice < self::$minimumForFreeCost) return self::$cost; + return 0; + } +} + +function shippingCost(int $basePrice) :int { + $cost = new ShippingCost($basePrice); + return $cost->amount(); +} + +echo '送料の料金は'.shippingCost(1000); +echo '送料の料金は'.shippingCost(5000); + + + + diff --git a/PrinciplesSystemDesign/chap2/guard.php b/PrinciplesSystemDesign/chap2/guard.php new file mode 100644 index 0000000..7e1ecf7 --- /dev/null +++ b/PrinciplesSystemDesign/chap2/guard.php @@ -0,0 +1,10 @@ += 18) return 'OK!'; + if($age > 15) return 'So So'; + if($age < 18) return 'NO!;'; +} + +$result = free(18); +echo $result; \ No newline at end of file diff --git a/PrinciplesSystemDesign/chap3/Person.php b/PrinciplesSystemDesign/chap3/Person.php new file mode 100644 index 0000000..0864be9 --- /dev/null +++ b/PrinciplesSystemDesign/chap3/Person.php @@ -0,0 +1,22 @@ +firstName = $firstName; + $this->lastName = $lastName; + } + + public function fullName() { + return "MyName is {$this->firstName}.{$this->lastName}"; + } + +} + +$endo = new Person("Endo","Futoshi"); +echo $endo->fullName(); +