在开发大型系统过程中,往往会出现这样一种情况:
我有一部分基础数据,是类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(); /** *简单工厂模式 * */ 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(); /** * 工厂方法模式 **/ 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]); /** * 抽象工厂模式 * */ 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]);