Skip to content

Instantly share code, notes, and snippets.

@Shipu
Created July 28, 2021 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shipu/9a6ce07c294a4b6503548af70dca347b to your computer and use it in GitHub Desktop.
Save Shipu/9a6ce07c294a4b6503548af70dca347b to your computer and use it in GitHub Desktop.
Strategy Pattern
<?php
interface Coffee
{
public function make();
}
class Cappuccino implements Coffee
{
public function make()
{
var_dump("this is cappuccino..");
}
}
class Latte implements Coffee
{
public function make()
{
var_dump("this is hazel nut latte..");
}
}
class BlackCoffee implements Coffee
{
public function make()
{
var_dump("this is black coffee..");
}
}
class Application
{
public function makeCoffee(Coffee $coffee)
{
return $coffee->make();
}
}
$app = new Application();
$app->makeCoffee(new Latte());
$app->makeCoffee(new Cappuccino());
$app->makeCoffee(new BlackCoffee());
<?php
interface Logger
{
public function log($data);
}
class DatabaseLogger implements Logger
{
public function log($data)
{
var_dump("this is database log");
var_dump($data);
}
}
class FileLogger implements Logger
{
public function log($data)
{
var_dump("this is file log");
var_dump($data);
}
}
class WebServiceLogger implements Logger
{
public function log($data)
{
var_dump("this is web service log");
var_dump($data);
}
}
class Application
{
public function log($data, Logger $logger = null)
{
$logger = $logger ?: new ConsoleLogger();
return $logger->log($data);
}
}
$app = new Application();
$app->log("log data", new FileLogger());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment