1 preface on the definition of the strategy model, the composition of the model, the core idea of the model, the schema diagram, the program architecture and other basic knowledge introduction. Please refer to my other blog "(three) design mode of the PHP project application (Strategy mode: Shopping mall cashier System)": http://blog.csdn.net/clevercode/article/details/45722661.
2 Project Application 2.1 Requirements Description A company is a gold partner for Ford and Honda, and now requires the development of an autonomous driving system that can be unmanned if installed on a vehicle, with the ability to start, turn, and stop. The system can be used on Ford and Honda vehicles. These two brands of cars use the system to achieve autonomous driving, and the system can be well ported to other cars ("network")
2.2 Requirements analysis According to requirements, can be automated driving all the operation of the design into an interface algorithm, Honda, Ford, Jeep and other applications need to apply autonomous driving system of the car, all inherit this interface, implement different strategy algorithm. Then design the automatic driving environment class, to maintain the automobile instance.
2.3 Design Architecture Diagram
2.4 Program source code download http://download.csdn.net/detail/clevercode/8700349
2.5 Program Description
1) strategy.php
<?php/** * strategy.php * * Strategy class * * Copyright (c) http://blog.csdn.net/CleverCode * * Modification History: *---- ----------------* 2015/5/14, by Clevercode, Create * *///define the actions that autonomous vehicles can achieve interface icar{//Start public Function run ( ); Stop public Function stop (); Turn public function turn ();} Honda Auto Class Hondacar implements icar{/** * start * * @return void */Public Function run () {EC Ho "Honda Car started up! \ r \ n "; }/** * Turn * * @return void */Public function turn () {echo "Honda's turning!" \ r \ n "; }/** * Stop * * @return void */Public Function Stop () {echo "Honda Car stopped! \ r \ n "; }}//Ford car Class Fordcar implements icar{/** * start * * @return void */Public Function run () {E Cho "Ford car started up! \ r \ n "; }/** * Turn * * @return void */Public function turn () {echo "Ford's turning!" \ r \ n "; }/** * Stop * * @return void * */public functIon Stop () {echo ' Ford car stopped! \ r \ n "; }}//Jeep Car Class Jeepcar implements icar{/** * start * * @return void */Public Function run () {E Cho "Jeep started up! \ r \ n "; }/** * Turn * * @return void */Public function turn () {echo "Jeep turned! \ r \ n "; }/** * Stop * @return void */Public Function Stop () {echo ' Jeep stopped! \ r \ n "; }}
2) strategypattern.php
<?php/** * strategypattern.php * * design mode: Policy mode * * Copyright (c) http://blog.csdn.net/CleverCode * * Modification hi Story: *--------------------* 2015/5/14, by Clevercode, create * *///load All Strategy include_once (' strategy.php ');//Create an environment class, based on Different requirements call different policies class autosystem{//policy private $_car = null; /** * Constructor * * @param string $type type * @return void */Public function __construct ($type = null) { if (!isset ($type)) {return; } $this->setcar ($type); }/** * Set policy (simple factory with policy mode) * * @param string $type type * @return void */Public Function Setcar ( $type) {$cs = null; Switch ($type) {//Honda car case ' Honda ': $cs = new Hondacar (); Break Ford car case ' Ford ': $cs = new Fordcar (); Break Jeep car case ' Jeep ': $cs = new Jeepcar (); Break } $this->_car = $cs; }/** * Start the car * * @return void */Public Function Runcar () {$this->_car->run (); }/** * Turn car * * @return void */Public Function Turncar () {$this->_car->turn (); }/** * Stop car * * @return void */Public Function Stopcar () {$this->_car->stop (); }}/* * Client class * Allows the client and business logic to be separated as much as possible, reducing the coupling between client and business logic algorithms, making the algorithm of business logic more portable */class client{public Function main () {$autoSy Stem = new Autosystem (); Select Automotive Application Autopilot system $autoSystem->setcar (' Honda '); $autoSystem->runcar (); $autoSystem->turncar (); $autoSystem->stopcar (); $autoSystem->setcar (' Ford '); $autoSystem->runcar (); $autoSystem->turncar (); $autoSystem->stopcar (); $autoSystem->setcar (' Jeep '); $autoSystem->runcar (); $autoSYstem->turncar (); $autoSystem->stopcar (); }}/** * Program Entry */function start () {$client = new client (); $client->main ();} Start ();? >
3 Summary 1) in strategy.php and strategypattern.php. If you need to extend multiple automotive application autopilot systems, simply inherit the icar interface to implement more classes, which are used in conjunction with the Simple factory model. Is that the program is clearer.
2) This is also the embodiment of the typical dependency inversion principle. Now the Autosystem system relies on the abstraction of icar, which has nothing to do with the specific implementation details Hondacar, Fordcar, Jeepcar, so the changes to the implementation details will not affect Autosystem. For implementation details as long as the implementation of ICAR, that is, implementation details rely on icar abstraction.
3) Dependency inversion principle: A. High-level modules should not be dependent on low levels of modules, they should all rely on abstraction. B. Abstractions should not be dependent on specifics, but should be dependent on abstraction.
Copyright Notice:
1) original works, from "Clevercode's blog" , please be sure to mention the following original address when reproduced , otherwise hold the copyright legal responsibility.
2) Original address : http://blog.csdn.net/clevercode/article/details/45723773 ( reprint must indicate this address ).
3) welcome everyone to pay attention to my blog more wonderful content: Http://blog.csdn.net/CleverCode.
(iv) design mode of PHP Project application (Strategy mode: Autonomous driving system)