<?php namespace DesignModel; /** * Class TestClass * @package DesignModel */ class TestSimpleCalculateClass { /** * 計算面積 * @param $param * @return int */ public function calculateArea(int $param) { if (empty($param)) { return 0; } // 計算過程暴露,若是計算方式複雜則代碼量不少,容易影響其餘類型面積計算 $roundnessArea = ceil($param * 2 * pi()); $squareArea = $param * $param; // 返回圓形和正方形的面積 return compact('roundnessArea', 'squareArea'); } } /** * Class TestClass * @package DesignModel */ class TestCalculateClass { /** * 計算面積 * @param $param * @return int */ public function calculateArea(int $param) { if (empty($param)) { return 0; } $roundnessArea = round($param * 2 * pi()); $squareArea = $param * $param; // 返回圓形和正方形的面積 return compact('roundnessArea', 'squareArea'); } } /** * Interface IDealNumeric * @package DesignModel */ interface IDealNumeric { /** * @param int $param * @return mixed */ public function calculateArea(int $param); } /** * 計算圓形面積 * Class SingleNumericClass * @package DesignModel */ class SingleRoundnessClass implements IDealNumeric { /** * @param int $param * @return int */ public function calculateArea(int $param) { // TODO: Implement dealNumeric() method. } } /** * 計算正方形面積 * Class SingleStringClass * @package DesignModel */ class SingleSquareClass implements IDealNumeric { /** * @param int $param * @return string */ public function calculateArea(int $param) { // TODO: Implement dealString() method. } } /** * Class SingleTestClass * @package DesignModel */ class SingleTestClass { public function calculateArea(int $param) { $roundnessArea = (new SingleRoundnessClass())->calculateArea($param); $squareArea = (new SingleSquareClass())->calculateArea($param); // 返回圓形和正方形的面積,不關心內部實現邏輯 return compact('roundnessArea', 'squareArea'); } }
設計模式的總綱
php
<?php namespace Factory\SimpleFactory; /** * Class ModelToCalculatePrice * @package Factory\SimpleFactory */ class ModelToCalculatePrice { /** * 這種方式寫的時候很簡單,可是若是之後增長類型或者計算邏輯變動,代碼量會不少,而且容易混亂 * 好比計算價格時須要根據會員等級計算折扣,就須要方法中增長參數 * @param string $type * @param int $count * @return float|int */ public function calculatePrice(string $type, int $count) { if ($type === 'vip') { $price = $count * 10; } else { $price = $count * 20; } return $price; } } /* ==================== 如下爲優化後代碼 ==================== */ /** * Class CalculatePrice * @package Factory\SimpleFactory */ abstract class CalculatePrice { /** * 定義實現方法 * @param $count * @return mixed */ abstract function calculate($count); } /** * Class User * @package Factory\SimpleFactory */ abstract class User { } /** * Class VipUser * @package Factory\SimpleFactory */ class VipUser extends User { /** * @var */ protected $integral; /** * @return mixed */ public function getIntegral() { return $this->integral; } } /** * Class VipCalculatePrice * @package Factory\SimpleFactory */ class VipCalculatePrice extends CalculatePrice { /** * @var VipUser */ protected $vipUser; /** * VipCalculatePrice constructor. * @param VipUser $vipUser */ public function __construct(VipUser $vipUser) { $this->vipUser = $vipUser; } /** * 實現計算 * @param $count * @return float|int|mixed */ function calculate($count) { if ($this->vipUser->getIntegral() > 0 && $this->vipUser->getIntegral() < 40) { return $count * 10; } else { return $count * 8; } } } /** * Class SvipCalculatePrice * @package Factory\SimpleFactory */ class SvipCalculatePrice extends CalculatePrice { /** * 能夠看到,不一樣子類都須要實現計算方法,可是內部實現邏輯能夠不一樣,且沒有耦合 * @param $count * @return float|int|mixed */ function calculate($count) { return $count * 5; } } /** * Class commonCalculatePrice * @package Factory\SimpleFactory */ class commonCalculatePrice extends CalculatePrice { /** * @param $count * @return float|int|mixed */ function calculate($count) { return $count * 20; } }
<?php namespace Factory\ReplaceModel; /** * Class User * @package Factory\SimpleFactory */ abstract class User { } /** * Class VipUser * @package Factory\SimpleFactory */ class VipUser extends User { /** * @var */ protected $integral; /** * @return mixed */ public function getIntegral() { return $this->integral; } } /** * Class CalculatePrice * @package Factory\SimpleFactory */ abstract class CalculatePrice { /** * @var User */ protected $user; /** * CalculatePrice constructor. * @param User $user */ public function __construct(User $user) { $this->user = $user; } /** * 定義實現方法 * @param $count * @return mixed */ abstract function calculate($count); } /** * Class VipCalculatePrice * @package Factory\SimpleFactory */ class VipCalculatePrice extends CalculatePrice { /** * 實現計算 * @param $count * @return float|int|mixed */ function calculate($count) { // 有子類出現的地方父類未必就能夠出現 if ($this->user->getIntegral() > 0 && $this->user->getIntegral() < 40) { return $count * 10; } else { return $count * 8; } } } // 任何父類出現的地方,子類都能實現 $vipCalculatePrice = new VipCalculatePrice(new VipUser()); $vipCalculatePrice->calculate(12);