Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions PrinciplesSystemDesign/README.md
Original file line number Diff line number Diff line change
@@ -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)

56 changes: 56 additions & 0 deletions PrinciplesSystemDesign/chap1/Quantity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

class Quantity
{
private const MIN =1;
private const MAX =100;
public $value;

public function __construct(int $value)
{
try{
if($value < self::MIN) {
throw new Exception('1未満です');
}elseif ($value > 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);
30 changes: 30 additions & 0 deletions PrinciplesSystemDesign/chap1/ShippingCost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

class ShippingCost
{
static $minimumForFreeCost = 5000;
static $cost = 500;

private $basePrice;

public function __construct(int $basePrice){
$this->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);




10 changes: 10 additions & 0 deletions PrinciplesSystemDesign/chap2/guard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

function free($age) {
if($age >= 18) return 'OK!';
if($age > 15) return 'So So';
if($age < 18) return 'NO!;';
}

$result = free(18);
echo $result;
22 changes: 22 additions & 0 deletions PrinciplesSystemDesign/chap3/Person.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

class Person
{
private $firstName;
protected $lastName;

public function __construct($firstName,$lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}

public function fullName() {
return "MyName is {$this->firstName}.{$this->lastName}";
}

}

$endo = new Person("Endo","Futoshi");
echo $endo->fullName();