Skip to content

Instantly share code, notes, and snippets.

@im4aLL
Created November 14, 2017 05:46
Show Gist options
  • Save im4aLL/fb65d6c0cb45fe198cc8f7006f125b46 to your computer and use it in GitHub Desktop.
Save im4aLL/fb65d6c0cb45fe198cc8f7006f125b46 to your computer and use it in GitHub Desktop.
Design pattern - strategy - example
<?php
interface MailServiceInterface {
public function sendEmail();
}
class SMTPService implements MailServiceInterface {
public function sendEmail()
{
echo 'Using SMTP mail service! <br>';
}
}
class OtherService implements MailServiceInterface {
public function sendEmail()
{
echo 'Using Other mail service! <br>';
}
}
class MailService {
protected $mailService;
public function __construct(MailServiceInterface $mailService)
{
$this->mailService = $mailService;
}
public function to($email) {
echo 'Sending email to - ' . $email . '<br>';
return $this;
}
public function message($message) {
echo 'Message body - ' . $message . '<br>';
return $this;
}
public function send()
{
$this->mailService->sendEmail();
}
}
$mailer = new MailService(new SMTPService());
$mailer->to('[email protected]')->message('Hi this is mail text')->send();
echo 'Another example <br>======================================================<br>';
interface Ability {
public function perform();
}
class FlyAbility implements Ability {
public function perform()
{
print_r('Fly ability <br>');
}
}
class WalkAbility implements Ability {
public function perform()
{
print_r('Walk ability <br>');
}
}
class Duck {
protected $ability;
public function addAbility(Ability $ability)
{
$this->ability[] = $ability;
return $this;
}
public function showAbilities()
{
foreach($this->ability as $ability) {
$ability->perform();
}
}
}
$normalDuck = new Duck();
$normalDuck->addAbility(new FlyAbility())
->addAbility(new WalkAbility())
->showAbilities();
$flyingDuck = new Duck();
$flyingDuck->addAbility(new FlyAbility())
->showAbilities();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment