Created
February 22, 2016 13:23
-
-
Save achristodoulou/4242bc03c925976ec4cf to your computer and use it in GitHub Desktop.
Fluent interface
This file contains hidden or 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 CaffeineOptionInterface { | |
/** | |
* @return MilkOptionInterface | |
*/ | |
public function decaff(); | |
/** | |
* @return MilkOptionInterface | |
*/ | |
public function regular(); | |
} | |
interface MilkOptionInterface { | |
/** | |
* @param double $ratio | |
* @return SugarOptionInterface | |
*/ | |
public function withMilk($ratio); | |
/** | |
* @return SugarOptionInterface | |
*/ | |
public function black(); | |
} | |
interface SugarOptionInterface { | |
/** | |
* @param $cubes | |
* @return BrewerInterface | |
*/ | |
public function withSugars($cubes); | |
/** | |
* @return BrewerInterface | |
*/ | |
public function bitter(); | |
} | |
interface BrewerInterface { | |
/** | |
* @return Coffee | |
*/ | |
public function brew(); | |
} | |
class CaffeineOption implements CaffeineOptionInterface | |
{ | |
/** | |
* @return MilkOptionInterface | |
*/ | |
public function decaff() | |
{ | |
echo "\n-decaff"; | |
return new MilkOption(); | |
} | |
/** | |
* @return MilkOptionInterface | |
*/ | |
public function regular() | |
{ | |
echo "\n-regular"; | |
return new MilkOption(); | |
} | |
} | |
class MilkOption implements MilkOptionInterface | |
{ | |
public function withMilk($ratio) | |
{ | |
echo "\n-withMilk($ratio)"; | |
return new SugarOption(); | |
} | |
public function black() | |
{ | |
echo "\n-black"; | |
return new SugarOption(); | |
} | |
} | |
class SugarOption implements SugarOptionInterface | |
{ | |
/** | |
* @param $cubes | |
* @return BrewerInterface | |
*/ | |
public function withSugars($cubes) | |
{ | |
echo "\n-withSugar($cubes)"; | |
return new Brewer(); | |
} | |
/** | |
* @return BrewerInterface | |
*/ | |
public function bitter() | |
{ | |
echo "\n-bitter"; | |
return new Brewer(); | |
} | |
} | |
class Brewer implements BrewerInterface | |
{ | |
/** | |
* @return Coffee | |
*/ | |
public function brew() | |
{ | |
echo "\n-brew\n"; | |
return new Coffee(); | |
} | |
} | |
class Coffee | |
{ | |
public function __construct() | |
{ | |
} | |
} | |
class CoffeeMaker | |
{ | |
/** | |
* @return CaffeineOption | |
*/ | |
public function prepare() | |
{ | |
return new CaffeineOption(); | |
} | |
} | |
echo "Preparing sweet coffee"; | |
$myCoffee = (new CoffeeMaker()) | |
->prepare() | |
->decaff() | |
->withMilk(10) | |
->withSugars(2) | |
->brew(); | |
echo "\nPreparing black coffee"; | |
$myCoffee = (new CoffeeMaker()) | |
->prepare() | |
->regular() | |
->black() | |
->bitter() | |
->brew(); |
Author
achristodoulou
commented
Feb 25, 2016
hasCaffeine = false;
return $this;
}
/**
* @return MilkOptionInterface
*/
public function regular()
{
$this->hasCaffeine = true;
return $this;
}
/**
* @param $ratio
* @return SugarOptionInterface
*/
public function withMilk($ratio)
{
$this->milkOption = $ratio;
return $this;
}
/**
* @return SugarOptionInterface
*/
public function black()
{
$this->milkOption = 0;
return $this;
}
/**
* @param $cubes
* @return BrewerInterface
*/
public function withSugars($cubes)
{
$this->sugar = $cubes;
return $this;
}
/**
* @return BrewerInterface
*/
public function bitter()
{
$this->sugar = 0;
return $this;
}
/**
* @return Coffee
*/
public function brew()
{
return new Coffee($this->milkOption, $this->sugar, $this->hasCaffeine);
}
```
}
$myCoffee = (new CoffeeMaker())
->prepare()
->decaff()
->withMilk(1)
->bitter()
->brew();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment