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;
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('content-type:text/html;charset=utf-8'); abstract class Product { abstract public function getName(); } class ProductA extends Product { public function getName() { echo '这是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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor
