Skip to content

Instantly share code, notes, and snippets.

@kandran
Last active August 29, 2015 14:21
Show Gist options
  • Save kandran/4b1f80698da9bad8602b to your computer and use it in GitHub Desktop.
Save kandran/4b1f80698da9bad8602b to your computer and use it in GitHub Desktop.
Design pattern stratégie (http://kandran.fr/design-pattern-strategie/)
<?php
require_once("ApplicationInterface.php");
class ApplicationA implements ApplicationInterface{
public function run()
{
echo "<br/> Application A start running";
//some computation
echo "<br/> Application A finished";
}
}
<?php
require_once("ApplicationInterface.php");
class ApplicationB implements ApplicationInterface{
public function run()
{
echo "<br/> Application B start running";
//some other computation
echo "<br/> Application B finished";
}
}
<?php
interface ApplicationInterface
{
public function run();
}
<?php
require_once("ApplicationInterface.php");
class ApplicationStarter{
public function startApplication(ApplicationInterface $application)
{
$application->run(); //here we don't know the class name of application
}
}
<?php
require_once("ApplicationA.php");
require_once("ApplicationB.php");
require_once("ApplicationStarter.php");
$applicationStarterObject = new ApplicationStarter();
$application1 = new ApplicationA();
$application2 = new ApplicationB();
$application3 = new ApplicationA();
$applicationsToRun = array($application1, $application2 , $application3);
foreach($applicationsToRun as $application){
$applicationStarterObject->startApplication($application);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment