Last active
December 15, 2015 10:19
-
-
Save MirkoBonadei/5245326 to your computer and use it in GitHub Desktop.
builder-pattern-implemented
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 | |
class Device | |
{ | |
private $kind; | |
private $os; | |
public function __construct($kind, $os) | |
{ | |
$this->kind = $kind; | |
$this->os = $os; | |
} | |
public function kind() | |
{ | |
return $this->kind; | |
} | |
public function os() | |
{ | |
return $this->os; | |
} | |
} |
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 | |
class DeviceBuilder | |
{ | |
private $devicesDatabase; | |
private $userAgent; | |
public function __construct($devicesDatabase) | |
{ | |
$this->devicesDatabase = $devicesDatabase; | |
} | |
public function setUserAgent($userAgent) | |
{ | |
$this->userAgent = $userAgent; | |
} | |
public function build() | |
{ | |
// perform all the retrieval from $this->devicesDatabase before creating | |
// the instance of Device to return | |
} | |
} |
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 | |
// Usage... | |
$deviceBuilder = new DeviceBuilder( | |
new DeviceDatabase() | |
); | |
$deviceBuilder->setUserAgent($_SERVER['HTTP_USER_AGENT']); | |
$device = $deviceBuilder->build(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment