Home >Backend Development >PHP Problem >The difference between php factory mode and strategy mode

The difference between php factory mode and strategy mode

(*-*)浩
(*-*)浩Original
2019-06-06 15:12:444392browse

Factory is a creational pattern, its function is to create objects;

Strategy is a behavioral pattern, its function is to let an object choose one behavior among many behaviors;

The difference between php factory mode and strategy mode

Different concerns

A concern object creates an encapsulation of concern behavior

(recommended learning: PHP video tutorial)

Solving different problems

The factory pattern is a creational design pattern that accepts instructions , create an instance that meets the requirements; it mainly solves the unified distribution of resources, completely independent of the creation of objects, so that the creation of objects has nothing to do with the specific use of the customer. Mainly used in multiple database selection, class library file loading, etc.

The strategy mode is to solve the switching and expansion of strategies. To put it more concisely, it defines strategy families and encapsulates them separately so that they can be replaced with each other. The strategy pattern makes the changes of strategies independent. For customers using the strategy.

The factory is equivalent to a black box, and the strategy is equivalent to a white box;

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
abstract class Product {
abstract public function getName();
}
class ProductA extends Product  {
public function getName() {
echo &#39;这是A商品<br>';
}
}
class ProductB extends Product {
public function getName() {
echo '这是B商品<br>';
}
}
//工厂模式
class ProductFactory {
public static function create($num) {
switch($num) {
case 1:
return new ProductA();
case 2:
return new ProductB();
}
return null;
}
}
//传递不同的参数获取不同的对象
$obj=ProductFactory::create(1);
$obj->getName(); //这是A商品
$obj=ProductFactory::create(2);
$obj->getName(); //这是B商品
 
//=======策略模式
<?php
header('content-type:text/html;charset=utf-8');
interface IStrategy {
function ontheway();
}
class Walk implements IStrategy {
public function ontheway() {
echo '走着去<br>';
}
}
class Bick implements IStrategy {
public function ontheway() {
echo '骑自行车去<br>';
}
}
class Bus implements IStrategy {
public function ontheway() {
echo '坐巴士去<br>';
}
}
//策略模式,传递不同的参数,调用不同的策略
class Strategy{
public function getWay(IStrategy $obj) {
$obj->ontheway();
}
}
$obj=new Strategy();
$obj->getWay(new Walk); //走着去
$obj->getWay(new Bick); //骑自行车去
$obj->getWay(new Bus); //坐巴士去

More PHP related technologies Article, please visit the PHP Graphic Tutorial column to learn!

The above is the detailed content of The difference between php factory mode and strategy mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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