Skip to content

Instantly share code, notes, and snippets.

@MirkoBonadei
Last active December 15, 2015 10:19
Show Gist options
  • Save MirkoBonadei/5245326 to your computer and use it in GitHub Desktop.
Save MirkoBonadei/5245326 to your computer and use it in GitHub Desktop.
builder-pattern-implemented
<?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;
}
}
<?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
}
}
<?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