Design Patterns — 2. 2. Structural Design Patterns (Yapısal…
By: Ercan Çavuşoğlu
Tags: Design Patterns

2. Structural Design Patterns (Yapısal Tasarım Kalıpları)

Adapter Pattern

<?php

namespace Structural\Adapter;



interface LogInterface

{

public function log(string $message);

}



class GrayLogger implements LogInterface

{

public function log(string $message)

{

echo "Graylogger";

}

}



class EnuygunLoggerBundle

{

public function logMessage(string $message)

{

echo "EnuygunLogger:" . $message;

}

}



class EnuygunLoggerBundleAdapter implements LogInterface

{

public function log(string $message)

{

$logger = new EnuygunLoggerBundle();

$logger->logMessage($message);

}

}



class ProductManager

{

/**@var LogInterface */

protected $logger;



/**

* ProductManager constructor.

* @param LogInterface $logger

*/

public function __construct(LogInterface $logger)

{

$this->logger = $logger;

}



public function save()

{

$this->logger->log("Loglama başladı");

echo "Save";

}

}



class Program

{

public function run()

{

$productManager = new ProductManager(new EnuygunLoggerBundleAdapter());

$productManager->save();



$productManager1 = new ProductManager(new GrayLogger());

$productManager1->save();

}

}



$program = new Program();

$program->run();

Bridge Design Pattern

…

<?php



namespace Structural\Bridge;



abstract class MessageSenderBase

{

public function save()

{

echo "saved";

}



abstract public function send(Body $body);

}



class Body

{

public $title;

public $text;

}



class SmsSender extends MessageSenderBase

{

public function send(Body $body)

{

echo $body->text . " was send wia SmsSender";

}

}



class EMailSender extends MessageSenderBase

{

public function send(Body $body)

{

echo $body->text . " was send wia EMailSender";

}

}



class CustomerManager

{

/**@var MessageSenderBase $messageSender */

protected $messageSender;



/**

* CustomerManager constructor.

* @param MessageSenderBase $messageSender

*/

public function __construct(MessageSenderBase $messageSender)

{

$this->messageSender = $messageSender;

}



public function updateCustomer($body)

{

$this->messageSender->send($body);

echo "Güncellendi";

}

}



class Program

{

public function run()

{

$customerManager2 = new CustomerManager(new EMailSender());

$body2 = new Body();

$body2->title = "Üyelik onayla";

$customerManager2->updateCustomer($body2);



$customerManager = new CustomerManager(new SmsSender());

$body = new Body();

$body->title = "Üyelik ok";

$customerManager->updateCustomer($body);

}

}



$program = new Program();

$program->run();

Composite Design Pattern

….

<?php



namespace Structural\Composite;



use ArrayIterator;



interface PersonInterface

{

}



class Employee implements PersonInterface

{

public $name;

public $subOrdinates;



public function __construct(string $name = "")

{

$this->subOrdinates = new ArrayIterator();

$this->name = $name;

}



public function getSubOrdinates()

{

return $this->subOrdinates;

}



public function addSubOrdinate(PersonInterface $person)

{

$this->subOrdinates->append($person);

}



public function removeSubOrdinate(PersonInterface $person)

{

$this->subOrdinates->offsetUnset($person);

}



public function getSubOrdinate(int $index)

{

$this->subOrdinates->offsetGet($index);

}

}



class StakeHolders extends Employee

{

public $name;



}





class Program

{

public function run()

{

$employee1 = new Employee("Alex");

$employee2 = new Employee("Hagi");

$employee3 = new Employee("Sergen");



$employee1->addSubOrdinate($employee2);

$employee1->addSubOrdinate($employee3);



$employee4 = new Employee("Emre");

$employee10 = new StakeHolders("Paydaş Terim");

$employee2->addSubOrdinate($employee4);

$employee2->addSubOrdinate($employee10);



$employee5 = new Employee("Selçuk");

$employee1->addSubOrdinate($employee5);



foreach ($employee1->getSubOrdinates() as $employee) {

echo $employee->name;



foreach ($employee->getSubOrdinates() as $employee1Items) {

echo $employee1Items->name;

}

}



}

}



$program = new Program();

$program->run();

Decorator Design Pattern

<?php

namespace Structural\Decorator;



abstract class CarBase

{

public $make;

public $type;

public $hirePrice;

}



class PersonalCar extends CarBase

{

}



class CommercialCar extends CarBase

{

}



abstract class CarDecoratorBase extends CarBase

{

/**@var CarBase */

protected $carBase;



/**

* CarDecoratorBase constructor.

* @param CarBase $carBase

*/

public function __construct(CarBase $carBase)

{

$this->carBase = $carBase;

}

}



class SpecialOffer extends CarDecoratorBase

{

public function getHirePrice()

{

return $this->carBase->hirePrice * 0.9;

}

}





class Program

{

public function run()

{

$personalCar = new PersonalCar();

$personalCar->hirePrice = 2000;

$personalCar->make = "Fiat";

$personalCar->type = "Sedan";



$SpecialOffer = new SpecialOffer($personalCar);

echo $SpecialOffer->getHirePrice();

}

}



$program = new Program();

$program->run();

Facade Design Pattern

…

<?php

namespace Structural\Facade;



interface LogInterface

{

public function log();

}



class Logging implements LogInterface

{

public function log()

{

echo "Logged";

}

}



interface CacheInterface

{

public function cache();

}



class Caching implements CacheInterface

{

public function cache()

{

echo "Cached";

}

}



interface AuthorizeInterface

{

public function checkUser();

}



class Authorize implements AuthorizeInterface

{

public function checkUser()

{

echo "User Checked";

}

}



#SONRADAN EKLEDİK DİYELİM

interface ValidateInterface

{

public function validate();

}



class CreditCardValidate implements ValidateInterface

{

public function validate()

{

echo "Card Validated";

}

}





class CrossCuttingConcernsFacade

{

/**@var LogInterface */

public $logging;

/**@var CacheInterface */

public $cache;

/**@var AuthorizeInterface */

public $authorize;

/**@var ValidateInterface */

public $validate;



/**

* @TODO Use Dependency Injection

*

* CustomerManager constructor.

*/

public function __construct()

{

$this->logging = new Logging();

$this->cache = new Caching();

$this->authorize = new Authorize();

$this->validate = new CreditCardValidate();

}

}



class CustomerManager

{

/**@var CrossCuttingConcernsFacade */

protected $crossCuttingConcernsFacade;



/**

* CustomerManager constructor.

* @param CrossCuttingConcernsFacade $crossCuttingConcernsFacade

*/

public function __construct(CrossCuttingConcernsFacade $crossCuttingConcernsFacade)

{

$this->crossCuttingConcernsFacade = $crossCuttingConcernsFacade;

}



public function save()

{

$this->crossCuttingConcernsFacade->logging->log();

$this->crossCuttingConcernsFacade->cache->cache();

$this->crossCuttingConcernsFacade->authorize->checkUser();

}

}



class Program

{

public function run()

{

$customerManager = new CustomerManager(new CrossCuttingConcernsFacade());

$customerManager->save();

}

}



$program = new Program();

$program->run();

Proxy Design Pattern