Created
December 11, 2013 01:02
-
-
Save patrioticcow/7903369 to your computer and use it in GitHub Desktop.
Factory Pattern
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
abstract class User { | |
function __construct($name) | |
{ | |
$this->name = $name; | |
} | |
function getName() | |
{ | |
return $this->name; | |
} | |
// Permission methods | |
function hasReadPermission() | |
{ | |
return true; | |
} | |
function hasModifyPermission() | |
{ | |
return false; | |
} | |
function hasDeletePermission() | |
{ | |
return false; | |
} | |
// Customization methods | |
function wantsFlashInterface() | |
{ | |
return true; | |
} | |
protected $name = NULL; | |
} | |
class GuestUser extends User { | |
} | |
class CustomerUser extends User { | |
function hasModifyPermission() | |
{ | |
return true; | |
} | |
} | |
class AdminUser extends User { | |
function hasModifyPermission() | |
{ | |
return true; | |
} | |
function hasDeletePermission() | |
{ | |
return true; | |
} | |
function wantsFlashInterface() | |
{ | |
return false; | |
} | |
} | |
class UserFactory { | |
private static $users = array("Andi"=>"admin", "Stig"=>"guest", | |
"Derick"=>"customer"); | |
static function Create($name) | |
{ | |
if (!isset(self::$users[$name])) { | |
// Error out because the user doesn't exist | |
} | |
switch (self::$users[$name]) { | |
case "guest": return new GuestUser($name); | |
case "customer": return new CustomerUser($name); | |
case "admin": return new AdminUser($name); | |
default: // Error out because the user kind doesn't exist | |
} | |
} | |
} | |
function boolToStr($b) | |
{ | |
if ($b == true) { | |
return "Yes\n"; | |
} else { | |
return "No\n"; | |
} | |
} | |
function displayPermissions(User $obj) | |
{ | |
print $obj->getName() . "'s permissions:\n"; | |
print "Read: " . boolToStr($obj->hasReadPermission()); | |
print "Modify: " . boolToStr($obj->hasModifyPermission()); | |
print "Delete: " . boolToStr($obj->hasDeletePermission()); | |
} | |
function displayRequirements(User $obj) | |
{ | |
if ($obj->wantsFlashInterface()) { | |
print $obj->getName() . " requires Flash\n"; | |
} | |
} | |
$logins = array("Andi", "Stig", "Derick"); | |
foreach($logins as $login) { | |
displayPermissions(UserFactory::Create($login)); | |
displayRequirements(UserFactory::Create($login)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment