标签:
策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。
<?php class Client { public function insertData() { $context = new Context(new DataEntry()); $context->algorithm(); } public function findData() { $context = new Context(new SearchData()); $context->algorithm(); } public function showAll() { $context = new Context(new DisplayData()); $context->algorithm(); } public function changeData() { $context = new Context(new UpdateData()); $context->algorithm(); } public function killer() { $context = new Context(new DeleteRecord()); $context->algorithm(); } }
<?php function __autoload($class_name) { include $class_name . ‘.php‘; } $trigger = new Client(); $trigger->insertData();
<?php function __autoload($class_name) { include $class_name . ‘.php‘; } $trigger = new Client(); $trigger->showAll();
<?php function __autoload($class_name) { include $class_name . ‘.php‘; } $trigger = new Client(); $trigger->findData();
<?php function __autoload($class_name) { include $class_name . ‘.php‘; } $trigger = new Client(); $trigger->changeData();
<?php function __autoload($class_name) { include $class_name . ‘.php‘; } $trigger = new Client(); $trigger->killer();
<?php class Context { private $strategy; public function __construct(IStrategy $strategy) { $this->strategy = $strategy; } public function algorithm() { $this->strategy->algorithm(); } }
<?php interface IStrategy { public function algorithm(); }
<?php class DataEntry implements IStrategy { public function algorithm() { $hookup = UniversalConnect::doConnect(); $test = $hookup->real_escape_string($_POST[‘data‘]); echo "该数据已经输入: " . $test .‘<br />‘; } }
<?php class Display implements IStrategy { public function algorithm() { $test = "这里是所有的数据"; echo $test .‘<br />‘; } }
<?php class SearchData implements IStrategy { public function algorithm() { $hookup = UniversalConnect::doConnect(); $test = $hookup->real_escape_string($_POST[‘data‘]); echo "这是你要查询的数据: " . $test .‘<br />‘; } }
<?php class UpdateData implements IStrategy { public function algorithm() { $hookup = UniversalConnect::doConnect(); $test = $hookup->real_escape_string($_POST[‘data‘]); echo "新的数据是: " . $test .‘<br />‘; } }
<?php class DeleteData implements IStrategy { public function algorithm() { $hookup = UniversalConnect::doConnect(); $test = $hookup->real_escape_string($_POST[‘data‘]); echo "该记录: " . $test .‘已经被删除<br />‘; } }
<?php class SecureData { private $changeField; private $company; private $devdes; private $device; private $disappear; private $field; private $hookup; private $lang; private $newData; private $oldData; private $plat; private $style; private $term; private $dataPack; public function enterData() { $this->hookup=UniversalConnect::doConnect(); $this->company = $this->hookup->real_escape_string($_POST[‘company‘]); $this->devdes = $this->hookup->real_escape_string($_POST[‘devdes‘]); $this->lang = $this->hookup->real_escape_string($_POST[‘lang‘]); $this->plat = $this->hookup->real_escape_string($_POST[‘plat‘]); $this->style = $this->hookup->real_escape_string($_POST[‘style‘]); $this->device = $this->hookup->real_escape_string($_POST[‘device‘]); $this->dataPack = array( $this->company, $this->devdes, $this->lang, $this->plat, $this->style, $this->device ); $this->hookup->close(); } public function conductSearch() { $this->hookup=UniversalConnect::doConnect(); $this->field = $this->hookup->real_escape_string($_POST[‘field‘]); $this->term = $this->hookup->real_escape_string($_POST[‘term‘]); $this->dataPack = array( $this->field, $this->term ); $this->hookup->close(); } public function makeChange() { $this->hookup=UniversalConnect::doConnect(); $this->changeField = $this->hookup->real_escape_string($_POST[‘update‘]); $this->oldData = $this->hookup->real_escape_string($_POST[‘old‘]); $this->newData = $this->hookup->real_escape_string($_POST[‘new‘]); $this->dataPack = array( $this->changeField, $this->oldData, $this->newData ); $this->hookup->close(); } public function removeRecord() { $this->hookup=UniversalConnect::doConnect(); $this->disappear = $this->hookup->real_escape_string($_POST[‘delete‘]); $this->dataPack = array($this->disappear); $this->hookup->close(); } public function setEntry() { return $this->dataPack; } }
<?php interface IStrategy { const TABLENOW = "survey"; public function algorithm(Array $dataPack); }
<?php class Client { public function insertData() { $secure = new SecureData(); $context = new Context(new DataEntry()); $secure->enterData(); $context->algorithm($secure->setEntry()); } public function findData() { $secure = new SecureData(); $context = new Context(new SearchData()); $secure->conductSearch(); $context->algorithm($secure->setEntry()); } public function showAll() { $dummy = array(0); $context = new Context(new DisplayData()); $context->algorithm($dummy); } public function changeData() { $secure = new SecureData(); $context = new Context(new UpdateData()); $secure->makeChange(); $context->algorithm($secure->setEntry()); } public function killer() { $secure = new SecureData(); $context = new Context(new DeleteRecord()); $secure->removeRecord(); $context->algorithm(); } }
<?php class Context { private $strategy; private $dataPack; public function __construct(IStrategy $strategy) { $this->strategy = $strategy; } public function algorithm(Array $dataPack) { $this->dataPack = $dataPack; $this->strategy->algorithm($this->dataPack); } }
<?php class DataEntry implements IStrategy { private $tableMaster; private $dataPack; private $hookup; private $sql; public function algorithm(Array $dataPack) { $this->dataPack = $dataPack; $comval = $this->dataPack[0]; $devdesval = $this->dataPack[1]; $langval = $this->dataPack[2]; $platval = $this->dataPack[3]; $cstyleval = $this->dataPack[4]; $deviceval = $this->dataPack[5]; $this->tableMaster = IStrategy::TABLENOW; $this->hookup = UniversalConnect::doConnect(); $this->sql = "INSERT INTO $this->tableMaster ( company, devdes, lang, plat, style, device ) VALUES ( ‘$comval‘, ‘$devdesval‘, ‘$langval‘, ‘$platval‘, ‘$cstyleval‘, ‘$deviceval‘ )"; if($this->hookup->query($this->sql)) { printf("成功插入数据到表:$this->tableMaster<br />"); } else { printf("无效的SQL: %s <br /> 语句是: %s<br />",$this->hookup->error,$this->sql); exit; } $this->hookup->close(); } }
<?php class DisplayData implements IStrategy { private $tableMaster; private $hookup; public function algorithm(Array $dataPack) { $this->tableMaster = IStrategy::TABLENOW; $this->hookup = UniversalConnect::doConnect(); $sql = "SELECT * FROM $this->tableMaster"; if($result = $this->hookup->query($sql)) { printf("查询返回%d条记录<br />",$result->num_rows); echo "<table>"; while($finfo = mysqli_fetch_field($result)) { echo "<th>{$finfo->name}</th>"; } while($row = mysqli_fetch_row($result)) { echo "<tr>"; foreach($row as $ceil) { echo "<td>$ceil</td>"; } echo "</tr>"; } echo "</table>"; $result->close(); } } }
<?php class SearchData implements IStrategy { private $tableMaster; private $dataPack; private $hookup; private $sql; public function algorithm(Array $dataPack) { $this->tableMaster = IStrategy::TABLENOW; $this->hookup = UniversalConnect::doConnect(); $this->dataPack = $dataPack; $field = $this->dataPack[0]; $term = $this->dataPack[1]; $this->sql = "SELECT * FROM $this->tableMaster WHERE $field=‘$term‘"; if($result = $this->hookup->query($this->sql)) { echo "<table>"; while($finfo = mysqli_fetch_field($result)) { echo "<th>{$finfo->name}</th>"; } while($row = mysqli_fetch_row($result)) { echo "<tr>"; foreach($row as $ceil) { echo "<td>$ceil</td>"; } echo "</tr>"; } echo "</table>"; $result->close(); } $this->hookup->close(); } }
<?php class UpdateData implements IStrategy { private $tableMaster; private $dataPack; private $hookup; private $sql; public function algorithm(Array $dataPack) { $this->tableMaster = IStrategy::TABLENOW; $this->hookup = UniversalConnect::doConnect(); $this->dataPack = $dataPack; $channelfield = $this->dataPack[0]; $oldData = $this->dataPack[1]; $newData = $this->dataPack[2]; $this->sql = "UPDATE $this->tableMaster SET changeField=‘$newData‘ WHERE changeField=‘$oldData‘"; if($result = $this->hookup->query($this->sql)) { echo "$channelfield 从 $oldData 变到 $newData"; } else { echo ‘Change faield: ‘.$this->hookup->error; } } }
<?php class DeleteData implements IStrategy { private $tableMaster; private $dataPack; private $hookup; private $sql; public function algorithm(Array $dataPack) { $this->tableMaster = IStrategy::TABLENOW; $this->hookup = UniversalConnect::doConnect(); $this->dataPack = $dataPack; $field = $this->dataPack[0]; $term = $this->dataPack[1]; $this->sql = "SELECT *FROM $this->tableMaster WHERE $field=‘$term‘"; if($result = $this->hookup->query($this->sql)) { echo "表$this->tableMaster 记录已经被删除"; } else { echo ‘删除失败‘.$this->error; } } }
标签:
原文地址:http://www.cnblogs.com/chenqionghe/p/4840595.html