Home > Backend Development > PHP Tutorial > PHP implementation of simple factory pattern c# simple factory pattern factory pattern java java simple factory pattern example

PHP implementation of simple factory pattern c# simple factory pattern factory pattern java java simple factory pattern example

WBOY
Release: 2016-07-29 08:54:59
Original
1376 people have browsed it

	简单工厂模式又叫静态工厂方法模式,主要作用是通过一个简单工厂类来实例化(创建)各个类的对象,而不需要通过new来实例化对象。优点在于,工厂类中包含了一定的逻辑判断,会根据客户端的选择条件动态实例化相关的类。缺点在于,当需要增加新的功能类时,需要去修改工厂类。

以下内容以一个简单的计算器程序作为案例分析。第一步,定义Operation,是一个父类,有两个属性,表示用于计算的两个参数。

<?php
/*
* 计算类
*/
class Operation{
	private $numA=0;
	private $numB=0;
	public function setNumA($numA)
	{
		$this->numA=$numA;
	}

	public function getNumA()
	{
		return $this->numA;
	}

	public function setNumB($numB)
	{
		$this->numB=$numB;
	}

	public function getNumB()
	{
		return $this->numB;
	}
}
?>
Copy after login

The second step is to define an interface, which declares the method to implement the operation

<?php
/*
*工厂接口
*/
interface InterOperate{
	function getResult();
}
?>
Copy after login

The third step is an addition operation class (omitting the subtraction class, multiplication class, trigger class, etc.)

<?php

/**
* 加法运算类
*/
include_once "IOperate.php";
include_once &#39;Operation.php&#39;;
class OperationAdd extends Operation implements InterOperate
{
	function getResult()
	{
		$result=$this->getNumA()+$this->getNumB();
		return $result;
	}
}

?>
Copy after login

Finally, define a simple factory class for creating object instances of various classes. Usually the objects returned by simple factory classes have a common parent class. In this example, the common parent class is the Operation class, and the addition class and the subtraction class are both subclasses of Operation.

<?php
include_once "OperationAdd.php";
include_once "OperationMinus.php";
class SimpleFactory {
	static function createAdd()
	{
		return new OperationAdd;
	}

	static function createMinus()
	{
		return new OperationMinus;
	}
}

?>
Copy after login

The client code is as follows:

<?php
/*
*	客户端代码
*/
include_once "OperationAdd.php";
include_once &#39;Operation.php&#39;;
include_once &#39;SimpleFactory.php&#39;;

$op=SimpleFactory::createAdd();
$op->setNumA(2);
$op->setNumB(4);
echo $op->getResult();

$om=SimpleFactory::createMinus();
$om->setNumA(45);
$om->setNumB(34);
echo "<br>";
echo $om->getResult();
?>
Copy after login

The above introduces the PHP implementation of the simple factory pattern, including the content of the simple factory pattern. I hope it will be helpful to friends who are interested in PHP tutorials.

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