search
Homephp教程PHP源码【新】php 设计模式-策略模式

代码片段(1) [全屏查看所有代码]

                       

                               

1. [文件]     test8.php ~ 4KB     

                           

<?php
/** ******************************************************
 * 策略模式:策略模式针对一组算法,将每一个算法封装到具有共同接口的独立的类中,    
 * 从而使得它们可以相互替换。策略模式使得算法可以 在不影响到客户端的情况下发生变化。
 * 策略模式把行为和环境分开。环境类负责维持和查询行为类,各种算法在具体的策略类中提供。
 * 由于算法和环境独立开来,算法的增减,修改都不会影响到环境和客户端。
 * *********************************************************
 */
/**
 * 数据源保存方式接口
 * 定义了数据源操作的四种基本方式 (增,删,改,查)
 */
interface data_source {
	public function add();
	public function delete();
	public function update();
	public function find();
}
/**
 * 数据库保存方式具体类
 * 具体的数据源接口的一种形式,该形式是把数据保存到数据库
 */
class db_source implements data_source {
	public function add() {
		// 在这里写把数据插入数据库的代码
		echo "增加数据到数据库";
	}
	public function delete() {
		// 在这里写把数据从数据库删除的代码
		echo "删除数据";
	}
	public function update() {
		// 在这里写修改数据库中的数据的代码
		echo "修改数据";
	}
	public function find() {
		// 在这里从数据库查找数据代码
		echo "查找数据";
	}
}
/**
 * 文件保存方式具体类
 * 具体的数据源接口的另一种形式,该形式是把数据保存到文件
 */
class file_source implements data_source {
	public function add() {
		// 在这里写把数据插入文件的代码
		echo "增加数据到文件";
	}
	public function delete() {
		// 在这里写把数据从文件删除的代码
		echo "删除数据";
	}
	public function update() {
		// 在这里写修改文件中的数据的代码
		echo "修改数据";
	}
	public function find() {
		// 在这里从文件查找数据代码
		echo "查找数据";
	}
}
/*#*********************************#
 * 	当然你还可以增加别的数据保存方式,比如芒果DB								*					
 * 									*				
 * ##################################
 */
/*  
   	mongodb保存方式具体类
  	该形式是把数据保存到mongodb
 class mongodb_source implements data_source {
	public function add() {
		// 在这里写把数据插入文件的代码
		echo "增加数据到mongodb";
	}
	public function delete() {
		// 在这里写把数据从文件删除的代码
		echo "删除数据";
	}
	public function update() {
		// 在这里写修改文件中的数据的代码
		echo "修改数据";
	}
	public function find() {
		// 在这里从文件查找数据代码
		echo "查找数据";
	}
}
 */
/**
 * 策略类
 * 该类根据要求返回符合要求类别的的数据源
 * 本例 $strategyName = db_source 或者 $strategyName = file_source
 */
class strategy {
	// 当然项目的实际应用情况可能比较复杂经过一系列的逻辑处理才能返回符合要求的实例,
	// 这里只是演示,所以体现了最简单的情况
 	public function getInstance($strategyName) {
 		return new $strategyName;
 	}
}
/**
 * 主体事件处理类 
 */
class control {
	private $datasource;
	public function __construct($strategyName) {
		// 策略者根据 要求($strategyName)经过内部决策生成一种实例
		$this->datasource = strategy::getInstance($strategyName);
	}
	// 逻辑处理
	public function add() {
		$this->datasource->add();
	}
} 
// 测试代码 
//1:
$control = new control("db_source");
$control->add();
// return 增加数据到数据库
//2:
$control = new control("file_source");
$control->add();
// return 增加数据到文件
//3:
/* 
 * $control = new control("mongodb_source");
 * $control->add();
 * 	return 增加数据到mongodb
 */



?>

                               

                   

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool