Created
July 28, 2021 06:53
-
-
Save Shipu/9a6ce07c294a4b6503548af70dca347b to your computer and use it in GitHub Desktop.
Strategy Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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