Created
November 11, 2019 07:09
-
-
Save maks-rafalko/5c263f4d19bbc9a421e4f8025655160c to your computer and use it in GitHub Desktop.
Salary Strategy
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 SalaryInterface { | |
public function getSalary(): float; | |
} | |
class DynamicStrategy implements SalaryInterface { | |
/** | |
* @var int | |
*/ | |
private $hours; | |
/** | |
* @var float | |
*/ | |
private $perHour; | |
public function __construct(int $hours, float $perHour) | |
{ | |
$this->hours = $hours; | |
$this->perHour = $perHour; | |
} | |
public function getSalary(): float | |
{ | |
return $this->hours * $this->perHour; | |
} | |
} | |
class FixedStrategy implements SalaryInterface { | |
/** | |
* @var float | |
*/ | |
private $salary; | |
public function __construct(float $salary) | |
{ | |
$this->salary = $salary; | |
} | |
public function getSalary(): float | |
{ | |
return $this->salary; | |
} | |
} | |
class Employee { | |
// ... | |
public function __construct(/* ... */, SalaryInterface $salaryStrategy) | |
{ | |
} | |
public function getSalary(): float | |
{ | |
return $this->salaryStrategy->getSalary(); | |
} | |
} | |
class Developer {} | |
new Developer('Ivan', new FixedStrategy(1000)); | |
new Developer('Ivan', new DynamicStrategy(123, 4)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment