سلام
داشتم دیزاین پترن ها رو تمرین میکردم که گفتم کدها رو به اشتراک بگذارم. توضیحی برای کدها نمی گذارم چون به نظرم واضح هستن
دیزاین پترن استراتژی
<?php
interface ReaderInterface
{
public function start();
public function read();
public function stop();
}
interface WriterInterface
{
public function start();
public function write();
public function stop();
}
class DatabaseReader implements ReaderInterface
{
public function start()
{
echo "action started";
}
public function read()
{
echo "reading started";
}
public function stop()
{
echo "reading stopped";
}
}
class SpreadsheetReader implements ReaderInterface
{
public function start()
{
echo "action started";
}
public function read()
{
echo "reading started";
}
public function stop()
{
echo "reading stopped";
}
}
class CsvWriter implements WriterInterface
{
public function start()
{
echo "action started\n";
}
public function write()
{
echo "writing started\n";
}
public function stop()
{
echo "reading stopped\n";
}
}
class JsonWriter implements WriterInterface
{
public function start()
{
echo "action started\n";
}
public function write()
{
echo "writing started\n";
}
public function stop()
{
echo "reading stopped\n";
}
}
class Transformer
{
private function findReader($from)
{
$className = $from;
if (class_exists($className)) {
return new $className();
}
}
private function findWriter($to)
{
$className = $to;
if (class_exists($className)) {
return new $className();
}
}
public function transform(string $from, string $to)
{
$reader = $this->findReader($from);
$writer = $this->findWriter($to);
$reader->start();
$writer->start();
$reader->read();
$writer->write();
$writer->stop();
$reader->stop();
}
}
$obj = new Transformer();
$obj->transform('DatabaseReader','CsvWriter');
دیزاین پترن فکتوری
<?php
interface FriendFactoryInterface
{
public function create();
}
class FriendFactory implements FriendFactoryInterface
{
private $type;
public function __construct($type)
{
$this->type = $type;
}
public function create()
{
if ($this->type == 'uber') {
return $this->createUber();
} else if ($this->type == 'lyft') {
return $this->createLyft();
}
}
private function createUber()
{
$class = new stdClass();
$class->name = "Uber";
return $class;
}
private function createLyft()
{
$class = new stdClass();
$class->name = "Lyft";
return $class;
}
}
$obj = new FriendFactory('uber');
echo $obj->create()->name;
echo "\n";
$obj2 = new FriendFactory('lyft');
echo $obj2->create()->name;
دیزاین پترن دکوراتور
<?php
interface OpenerInterface
{
public function open();
}
class Door implements OpenerInterface
{
public function open()
{
return 'the door is opened.';
}
}
class SmartOpener implements OpenerInterface
{
private $opener;
public function __construct(OpenerInterface $opener)
{
$this->opener = $opener;
}
public function open()
{
echo $this->opener->open();
echo "\n";
echo 'add some new features.';
}
}
$door = new Door();
$smartDoor = new SmartOpener($door);
echo $smartDoor->open();
دیزاین پترن آداپتور
<?php
interface AdapterInterface
{
public function find(int $id);
public function findAll(array $criteria = []);
}
class MySqlAdapter implements AdapterInterface
{
public function find(int $id)
{
return 'data by id is fetched by MySQL.';
}
public function findAll(array $criteria = [])
{
return 'all data is fetched by MYSQL';
}
}
class SQLiteAdapter implements AdapterInterface
{
public function find(int $id)
{
return 'data by id is fetched by SQLite.';
}
public function findAll(array $criteria = [])
{
return 'all data is fetched by SQLite.';
}
}
class Storage
{
private $source;
public function __construct(AdapterInterface $source)
{
$this->source = $source;
}
public function getOne(int $id)
{
return $this->source->find($id);
}
public function getAll(array $criteria = [])
{
return $this->source->findAll($criteria);
}
}
$adapter = new SQLiteAdapter();
$storage = new Storage($adapter);
echo $storage->getOne(1);
echo "\n";
echo $storage->getAll();
امیدوارم که مفید باشه