
рд╕рд┐рджреНрдзрд╛рдВрдд SOLID рдПрдХ рдкреНрд░реЛрдЧреНрд░рд╛рдорд┐рдВрдЧ рдорд╛рдирдХ рд╣реИ рдЬрд┐рд╕реЗ рд╕рднреА рдбреЗрд╡рд▓рдкрд░реНрд╕ рдХреЛ рдЦрд░рд╛рдм рд╡рд╛рд╕реНрддреБрдХрд▓рд╛ рдмрдирд╛рдиреЗ рд╕реЗ рдмрдЪрдиреЗ рдХреЗ рд▓рд┐рдП рдЕрдЪреНрдЫреА рддрд░рд╣ рд╕реЗ рд╕рдордЭрдирд╛ рдЪрд╛рд╣рд┐рдПред рдЗрд╕ рдорд╛рдирдХ рдХрд╛ рд╡реНрдпрд╛рдкрдХ рд░реВрдк рд╕реЗ рдУрдУрдкреА рдореЗрдВ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдпрджрд┐ рд╕рд╣реА рддрд░реАрдХреЗ рд╕реЗ рд▓рд╛рдЧреВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ, рддреЛ рдпрд╣ рдХреЛрдб рдХреЛ рдЕрдзрд┐рдХ рдПрдХреНрд╕реНрдЯреЗрдВрд╕рд┐рдмрд▓, рддрд╛рд░реНрдХрд┐рдХ рдФрд░ рдкрдардиреАрдп рдмрдирд╛рддрд╛ рд╣реИред рдЬрдм рдХреЛрдИ рдбреЗрд╡рд▓рдкрд░ рдПрдХ рдПрдкреНрд▓рд┐рдХреЗрд╢рди рдмрдирд╛рддрд╛ рд╣реИ, рддреЛ рдЦрд░рд╛рдм рдЖрд░реНрдХрд┐рдЯреЗрдХреНрдЪрд░ рджреНрд╡рд╛рд░рд╛ рдирд┐рд░реНрджреЗрд╢рд┐рдд, рдХреЛрдб рдЕрдирдореНрдп рд╣реЛ рдЬрд╛рддрд╛ рд╣реИ, рдпрд╣рд╛рдВ рддрдХ тАЛтАЛрдХрд┐ рдЗрд╕рдореЗрдВ рдЫреЛрдЯреЗ-рдЫреЛрдЯреЗ рдмрджрд▓рд╛рд╡ рднреА рдмрдЧ рдкреИрджрд╛ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред рдЗрд╕рд▓рд┐рдП, рдЖрдкрдХреЛ SOLID рдХреЗ рд╕рд┐рджреНрдзрд╛рдВрддреЛрдВ рдХрд╛ рдкрд╛рд▓рди рдХрд░рдирд╛ рдЪрд╛рд╣рд┐рдПред
рдЙрдиреНрд╣реЗрдВ рдорд╛рд╕реНрдЯрд░ рдХрд░рдиреЗ рдореЗрдВ рдХреБрдЫ рд╕рдордп рд▓рдЧреЗрдЧрд╛, рд▓реЗрдХрд┐рди рдпрджрд┐ рдЖрдк рдЗрди рд╕рд┐рджреНрдзрд╛рдВрддреЛрдВ рдХреЗ рдЕрдиреБрд╕рд╛рд░ рдХреЛрдб рд▓рд┐рдЦрддреЗ рд╣реИрдВ, рддреЛ рдЗрд╕рдХреА рдЧреБрдгрд╡рддреНрддрд╛ рдмрдврд╝ рдЬрд╛рдПрдЧреА, рдФрд░ рдЖрдк рдПрдХ рдЕрдЪреНрдЫреЗ рд╕реЙрдлреНрдЯрд╡реЗрдпрд░ рдЖрд░реНрдХрд┐рдЯреЗрдХреНрдЪрд░ рдХреЗ рдирд┐рд░реНрдорд╛рдг рдореЗрдВ рдорд╣рд╛рд░рдд рд╣рд╛рд╕рд┐рд▓ рдХрд░реЗрдВрдЧреЗред
рдПрд╕рдУрдПрд▓рдЖрдИрдбреА рдХреЗ рд╕рд┐рджреНрдзрд╛рдВрддреЛрдВ рдХреЛ рд╕рдордЭрдиреЗ рдХреЗ рд▓рд┐рдП, рдЖрдкрдХреЛ рд╕реНрдкрд╖реНрдЯ рд░реВрдк рд╕реЗ рд╕рдордЭрдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реИ рдХрд┐ рдЗрдВрдЯрд░рдлреЗрд╕ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХреИрд╕реЗ рдХрд░реЗрдВред рдпрджрд┐ рдЖрдкрдХреЛ рдРрд╕реА рд╕рдордЭ рдирд╣реАрдВ рд╣реИ, рддреЛ рдкрд╣рд▓реЗ
рджрд╕реНрддрд╛рд╡реЗрдЬ рдкрдврд╝реЗрдВред
рдореИрдВ SOLID рдХреЛ рд╕рдмрд╕реЗ рд╕рд░рд▓ рддрд░реАрдХреЗ рд╕реЗ рд╕рдордЭрд╛рдКрдВрдЧрд╛, рдЗрд╕рд▓рд┐рдП рд╢реБрд░реБрдЖрддреА рд▓реЛрдЧреЛрдВ рдХреЗ рд▓рд┐рдП рдпрд╣ рдкрддрд╛ рд▓рдЧрд╛рдирд╛ рдЖрд╕рд╛рди рд╣реИред рд╣рдо рдПрдХ-рдПрдХ рдХрд░рдХреЗ рд╕рд┐рджреНрдзрд╛рдВрддреЛрдВ рдкрд░ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВрдЧреЗред
рдПрдХрд▓ рдЬрд┐рдореНрдореЗрджрд╛рд░реА рд╕рд┐рджреНрдзрд╛рдВрдд
рд╡рд░реНрдЧ рдкрд░рд┐рд╡рд░реНрддрди рдХреЗ рд▓рд┐рдП рдХреЗрд╡рд▓ рдПрдХ рдХрд╛рд░рдг рд╣реИред
рдПрдХ рд╡рд░реНрдЧ рдХреЛ рдХреЗрд╡рд▓ рдПрдХ рд╕рдорд╕реНрдпрд╛ рдХрд╛ рд╕рдорд╛рдзрд╛рди рдХрд░рдирд╛ рдЪрд╛рд╣рд┐рдПред рдЗрд╕рдХреЗ рдХрдИ рддрд░реАрдХреЗ рд╣реЛ рд╕рдХрддреЗ рд╣реИрдВ, рд▓реЗрдХрд┐рди рдЙрдирдХрд╛ рдЙрдкрдпреЛрдЧ рдХреЗрд╡рд▓ рдПрдХ рд╕рд╛рдорд╛рдиреНрдп рд╕рдорд╕реНрдпрд╛ рдХреЛ рд╣рд▓ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐рдпрд╛ рдЬрд╛рдирд╛ рдЪрд╛рд╣рд┐рдПред рд╕рднреА рддрд░реАрдХреЛрдВ рдФрд░ рдЧреБрдгреЛрдВ рдХреЛ рдПрдХ рдЙрджреНрджреЗрд╢реНрдп рдХреА рд╕реЗрд╡рд╛ рдХрд░рдиреА рдЪрд╛рд╣рд┐рдПред рдпрджрд┐ рдХрд┐рд╕реА рд╡рд░реНрдЧ рдХреЗ рдХрдИ рдЙрджреНрджреЗрд╢реНрдп рд╣реИрдВ, рддреЛ рдЙрд╕реЗ рдЕрд▓рдЧ-рдЕрд▓рдЧ рд╡рд░реНрдЧреЛрдВ рдореЗрдВ рд╡рд┐рднрд╛рдЬрд┐рдд рдХрд┐рдпрд╛ рдЬрд╛рдирд╛ рдЪрд╛рд╣рд┐рдПред
рдПрдХ рдЙрджрд╛рд╣рд░рдг рдкрд░ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВ:
<?php
namespace Demo;
use DB;
class OrdersReport
{
public function getOrdersInfo($startDate, $endDate)
{
$orders = $this->queryDBForOrders($startDate, $endDate);
return $this->format($orders);
}
protected function queryDBForOrders($startDate, $endDate)
{ // If we would update our persistence layer in the future,
// we would have to do changes here too. <=> reason to change!
return DB::table('orders')->whereBetween('created_at', [$startDate, $endDate])->get();
}
protected function format($orders)
{ // If we changed the way we want to format the output,
// we would have to make changes here. <=> reason to change!
return '<h1>Orders: ' . $orders . '</h1>';
}
}
. ? , (, ). .
, , , XML, JSON, HTML ..
:
<?php
namespace Report;
use Report\Repositories\OrdersRepository;
class OrdersReport
{
protected $repo;
protected $formatter;
public function __construct(OrdersRepository $repo, OrdersOutPutInterface $formatter)
{
$this->repo = $repo;
$this->formatter = $formatter;
}
public function getOrdersInfo($startDate, $endDate)
{
$orders = $this->repo->getOrdersWithDate($startDate, $endDate);
return $this->formatter->output($orders);
}
}
namespace Report;
interface OrdersOutPutInterface
{
public function output($orders);
}
namespace Report;
class HtmlOutput implements OrdersOutPutInterface
{
public function output($orders)
{
return '<h1>Orders: ' . $orders . '</h1>';
}
}
namespace Report\Repositories;
use DB;
class OrdersRepository
{
public function getOrdersWithDate($startDate, $endDate)
{
return DB::table('orders')->whereBetween('created_at', [$startDate, $endDate])->get();
}
}
/ (Open-closed Principle)
, .
(, , ) . , .
:
<?php
class Rectangle
{
public $width;
public $height;
public function __construct($width, $height)
{
$this->width = $width;
$this->height = $height;
}
}
class Circle
{
public $radius;
public function __construct($radius)
{
$this->radius = $radius;
}
}
class AreaCalculator
{
public function calculate($shape)
{
if ($shape instanceof Rectangle) {
$area = $shape->width * $shape->height;
} else {
$area = $shape->radius * $shape->radius * pi();
}
return $area;
}
}
$circle = new Circle(5);
$rect = new Rectangle(8,5);
$obj = new AreaCalculator();
echo $obj->calculate($circle);
,
AreaCalculator
. /, , .
? :
<?php
interface AreaInterface
{
public function calculateArea();
}
class Rectangle implements AreaInterface
{
public $width;
public $height;
public function __construct($width, $height)
{
$this->width = $width;
$this->height = $height;
}
public function calculateArea(){
$area = $this->height * $this->width;
return $area;
}
}
class Circle implements AreaInterface
{
public $radius;
public function __construct($radius)
{
$this->radius = $radius;
}
public function calculateArea(){
$area = $this->radius * $this->radius * pi();
return $area;
}
}
class AreaCalculator
{
public function calculate($shape)
{
$area = 0;
$area = $shape->calculateArea();
return $area;
}
}
$circle = new Circle(5);
$obj = new AreaCalculator();
echo $obj->calculate($circle);
,
AreaCalculator
.
(Liskov Substitution Principle)
тАЬData abstractionтАЭ 1987-. 1994-
:
╧Ж(x) тАФ x T. ╧Ж(y) y S, S тАФ T.
, , :
- .
- .
- .
1996- , :
, , , .
: / / .
, () , . , , , , , , . .
:
<?php
interface LessonRepositoryInterface
{
/**
* Fetch all records.
*
* @return array
*/
public function getAll();
}
class FileLessonRepository implements LessonRepositoryInterface
{
public function getAll()
{
// return through file system
return [];
}
}
class DbLessonRepository implements LessonRepositoryInterface
{
public function getAll()
{
/*
Violates LSP because:
- the return type is different
- the consumer of this subclass and FileLessonRepository won't work identically
*/
// return Lesson::all();
// to fix this
return Lesson::all()->toArray();
}
}
(Interface Segregation Principle)
, .
, , .
, .
:
<?php
interface workerInterface
{
public function work();
public function sleep();
}
class HumanWorker implements workerInterface
{
public function work()
{
var_dump('works');
}
public function sleep()
{
var_dump('sleep');
}
}
class RobotWorker implements workerInterface
{
public function work()
{
var_dump('works');
}
public function sleep()
{
// No need
}
}
RobotWorker
тАЩ ,
sleep
, . . :
<?php
interface WorkAbleInterface
{
public function work();
}
interface SleepAbleInterface
{
public function sleep();
}
class HumanWorker implements WorkAbleInterface, SleepAbleInterface
{
public function work()
{
var_dump('works');
}
public function sleep()
{
var_dump('sleep');
}
}
class RobotWorker implements WorkAbleInterface
{
public function work()
{
var_dump('works');
}
}
(Dependency Inversion Principle)
. .
. .
: , - .
, , , .
:
<?php
class MySQLConnection
{
/**
* db connection
*/
public function connect()
{
var_dump('MYSQL Connection');
}
}
class PasswordReminder
{
/**
* @var MySQLConnection
*/
private $dbConnection;
public function __construct(MySQLConnection $dbConnection)
{
$this->dbConnection = $dbConnection;
}
}
, ┬л ┬╗ ┬л ┬╗. .
, ,
MySQLConnection
PasswordReminder
,
MySQLConnection
.
PasswordReminder
MySQLConnection
.
MySQLConnection
MongoDBConnection
,
PasswordReminder
.
PasswordReminder
, - . ? :
<?php
interface ConnectionInterface
{
public function connect();
}
class DbConnection implements ConnectionInterface
{
/**
* db connection
*/
public function connect()
{
var_dump('MYSQL Connection');
}
}
class PasswordReminder
{
/**
* @var ConnectionInterface
*/
private $dbConnection;
public function __construct(ConnectionInterface $dbConnection)
{
$this->dbConnection = $dbConnection;
}
}
MySQLConnection
MongoDBConnection
.
PasswordReminder
,
PasswordReminder
.