您好,登录后才能下订单哦!
这篇文章主要介绍了PHP如何实现工厂模式,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
工厂模式
就是解决这样的一些情况的设计方法。
工厂模式是一种类,建立了一个工厂来根据所需来创建对象,这种方式在多态性编程中是很重要的,允许动态替换类,修改配置等。
/*基本工厂模式代码*/
<?php /** * 基本工厂模式 * */ class User { private $username; public function __construct($username) { $this->username = $username; } public function getUser() { return $this->username; } } class userFactory { static public function createUser() { return new User('Jack'); } } $user = userFactory::createUser();echo $user->getUser(); ?>
工厂模式分为:简单工厂模式、工厂方法模式、抽象工厂模式。
简单工厂模式,通过静态方法创建对象。可以理解成,只负责生产同一等级结构中的任何一个产品,但是不能新增产品。
<?php /** *简单工厂模式 * */ interface userProperties { function getUsername(); function getGender(); function getJob(); } class User implements 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 { static public 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 /** * 工厂方法模式 **/ interface userProperties { function getUsername(); function getGender(); function getJob(); } interface createUser { function create($properties); } class User implements 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 { private $user; public function __construct($properties = []) { $this->user = new User($properties['username'], $properties['gender'], $properties['job']); } public function getUser() { return $this->user; } } class FactoryMan implements createUser { function create($properties) { return new userFactory($properties); } } class FactoryWoman implements createUser { function create($properties) { return new userFactory($properties); } } class clientUser { static public function getClient($properties) { $fac = new FactoryMan; $man = $fac->create($properties); echo $man->getUser()->getUsername(); } } $employers = [ ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], ]; $user = clientUser::getClient($employers[0]); ?>
抽象工厂模式,提供一个创建一系列相关或者相互依赖的对象的接口。可以理解成,用来生产不用类型的全部产品,但是不能增加新品,支持增加新的类型。
<?php /** * 抽象工厂模式 * */ interface userProperties { function getUsername(); function getGender(); function getJob(); } interface createUser { //将对象的创建抽象成一个接口 function createOpen($properties);//内向创建 function createIntro($properties);//外向创建 } class User implements 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 { private $user; public function __construct($properties = []) { $this->user = new User($properties['username'], $properties['gender'], $properties['job']); } public function getUser() { return $this->user; } } class FactoryMan implements createUser { function createOpen($properties) { return new userFactory($properties); } function createIntro($properties) { return new userFactory($properties); } } class FactoryWoman implements createUser { function createOpen($properties) { return new userFactory($properties); } function createIntro($properties) { return new userFactory($properties); } } class clientUser { static public function getClient($properties) { $fac = new FactoryMan; $man = $fac->createOpen($properties); echo $man->getUser()->getUsername(); } } $employers = [ ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'], ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'], ]; $user = clientUser::getClient($employers[0]); ?>
感谢你能够认真阅读完这篇文章,希望小编分享的“PHP如何实现工厂模式”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。