时间:2021-07-01 10:21:17 帮助过:23人阅读
/** *简单工厂模式 * */ abstract class userProperties { function getUsername() { } function getGender() { } function getJob() { } } class User extends userProperties { private $username; private $gender; private $job; public function __construct($username, $gender, $job) { $this->username = $username; $this->gender = $gender; $this->job = $job; } public function getUsername() { return $this->username; } public function getGender() { return $this->gender; } public function getJob() { return $this->job; } } class userFactory { public static function createUser($properties = []) { return new User($properties['username'], $properties['gender'], $properties['job']); } } $employers = [ ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], ]; $user = userFactory::createUser($employers[0]); echo $user->getUsername();
简单工厂模式提供了专门的工厂类用于创建对象,将对象的创建和对象的使用分离开,它作为一种最简单的工厂模式在软件开发中得到了较为广泛的应用
相关推荐:
php简单工厂模式示例 php设计模式入门教程
PHP简单工厂模式、工厂方法模式和抽象工厂模式比较
星际争霸之php简单工厂模式
以上就是PHP设计模式之简单工厂模式的详细内容,更多请关注Gxl网其它相关文章!