Detailed explanation of the three forms of sample code of PHP factory pattern

黄舟
Release: 2023-03-06 16:58:01
Original
3329 people have browsed it

PHPFactory patternDetailed explanation of the three forms of sample code

<?php
// 简单工厂模式
interface ICar {
	function run();
}

class BMWCar implements ICar {
	
	public function run() {
		echo &#39;bmw run!<br/>&#39;;
	}
}

class AuDiCar implements ICar {
	
	public function run() {
		echo &#39;audi run!<br/>&#39;;
	}
}

/**
 * 简单工厂
 */
class FactorySimple {
	
	/*
	public static function createBmw() {
		return new BMWCar();
	}
	
	public static function createAudi() {
		return new AuDiCar();
	}
	*/
	
	public static function createCar($type) {
		switch($type) {
			case &#39;bmw&#39;:
				return new BMWCar();
			case &#39;audi&#39;:
				return new AuDiCar();
			default:
				throw new Exception(&#39;type error!&#39;);
		}
	}
}


// ------------------------------------------------------
// ------------------------------------------------------

// 工厂方法模式
interface CreateCar { //工厂类接口
	function create();
}

/**
 * bmw car factoyr
 */
class BmwFactory implements CreateCar {
	
	public function create() {
		return new BMWCar();
	}
}

/**
 * audi car factory
 */
class AuDiFactory implements CreateCar {
	
	public function create() {
		return new AuDiCar();
	}
}

// ------------------------------------------------------
// ------------------------------------------------------

// 抽象工厂模式

class WhiteBMWCar implements ICar {
	
	public function run() {
		echo &#39;white bmwcar run!<br/>&#39;;
	}
}

class BlackBMWCar implements ICar {
	
	public function run() {
		echo &#39;black bmwcar run!<br/>&#39;;
	}
}

class WhiteAuDiCar implements ICar {
	
	public function run() {
		echo &#39;white audicar run!<br/>&#39;;
	}
}

class BlackAuDiCar implements ICar {
	
	public function run() {
		echo &#39;black audicar run!<br/>&#39;;
	}
}

// 提供一系列的接口
interface ICarCreate {
	function createBmw();
	function createAuDi();
}

/**
 * create white car
 */
class WhiteCarFactory implements ICarCreate {
	
	// white bmw
	public function createBmw() {
		return new WhiteBMWCar();
	}
	
	// white audi
	public function createAuDi() {
		return new WhiteAuDiCar();
	}
}

/**
 * create black car
 */
class BlackCarFactory implements ICarCreate {
	
	// black bmw
	public function createBmw() {
		return new BlackBMWCar();
	}
	
	// black audi
	public function createAuDi() {
		return new BlackAuDiCar();
	}
}
Copy after login

Related articles:

A brief analysis of the PHP factory pattern

Simple instructions for using PHP factory mode

Analysis of the benefits of PHP factory mode

The above is the detailed content of Detailed explanation of the three forms of sample code of PHP factory pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template