PHP设计模式之工厂模式详解

更新:2017-10-27    编辑:冰寒    来源:GeGuai.com    人气:加载中...    字号:|

在开发大型系统时,往往会出现这样一种情况:

我有一部分根基数据,是类classA是从数据库A读取出来的,其他很多的功能都是基于这个根基数据来操作的。现在呢,我想把数据从数据库A变成从另外的数据源去获取,这时候,要改动起来就对比麻烦,要改动其他很多类的代码。这种设计显然是不够灵便的,,换句话说,就是紧耦合的,那么什么是紧耦合呢?紧耦合就是指系统中某个部分的函数或类严重依附于系统的其他部分中的函数或类的行为和结构。

这时,工厂模式的作用性就体现出来了。

工厂模式    

就是解决这样的一些情况的设计法子。

工厂模式是一种类,建立了一个工厂来根据所需来创建对象,这种方式在多态性编程中是很首要的,允许动态调换类,改动配置等。

/*基本工厂模式代码*/

<?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]); ?>

如有差错,请指正。


评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)

站点导航

您可能在找这些