"Power Academy Design Pattern PHP Video Tutorial" first introduces the design pattern, tells the use and importance of the design pattern, and explains in detail the occasions when the design pattern is applied. Next, a number of design patterns are introduced through code examples. Finally, a comprehensive and in-depth case analysis illustrates how to use design patterns to plan new applications, how to write these patterns in PHP language, and how to use the design patterns introduced in the video to modify and refactor existing code blocks.
Course playback address://m.sbmmt.com/course/424.html
The teacher’s teaching style:
The teacher’s lectures are simple, clear, layer-by-layer analysis, interlocking, rigorous argumentation, rigorous structure, using the logical power of thinking to attract people Students' attention and rational control of the classroom teaching process. By listening to teachers' lectures, students not only learn knowledge, but also receive thinking training, and are also influenced and influenced by teachers' rigorous academic attitude.
The more difficult point in this video is the factory method pattern, which is explained in detail:
1 Pattern introduction
Define an interface for creating objects, so that Subclasses decide which class to instantiate, and factory methods defer instantiation of a class to its subclasses.
2 Roles in the pattern
2.1 Abstract Factory (Creator) role: It is the core of the factory method pattern and has nothing to do with the application. Any factory class for objects created in the pattern must implement this interface.
2.2 Concrete Creator role: This is a concrete factory class that implements the abstract factory interface, contains logic closely related to the application, and is called by the application to create product objects. There are two such roles in the picture above: BulbCreator and TubeCreator.
2.3 Abstract product (Product) role: The super type of the object created by the factory method pattern, that is, the common parent class or commonly owned interface of the product object. In the picture above, this character is Light.
2.4 Concrete Product role: This role implements the interface defined by the abstract product role. A specific product is created by a specific factory, and there is often a one-to-one correspondence between them.
PHP source code design
_numberA = $num; } /** * 获取成员A * * @return double 数字 */ public function getNumberA(){ return $this->_numberA; } /** * 设置成员B * * @param double $num 数字 * @return void */ public function setNumberB($num){ $this->_numberB = $num; } /** * 获取成员B * * @return double 数字 */ public function getNumberB(){ return $this->_numberA; } /** * 获取运算结果 * * @return double 数字 */ public function getResult(){ return null; } } /*}}}*/ // 加法类 class OperationAdd extends Operation { /*{{{*/ /** * 获取运算结果 * * @return double 数字 */ public function getResult(){ return $this->_numberA + $this->_numberB; } } /*}}}*/ // 减法类 class OperationSub extends Operation { /*{{{*/ /** * 获取运算结果 * * @return double 数字 */ public function getResult(){ return $this->_numberA - $this->_numberB; } } /*}}}*/ //运算工厂 interface IFactory {/*{{{*/ public function createOperate(); }/*}}}*/ //加法工厂 class AddFactory implements IFactory {/*{{{*/ public function createOperate() { return new OperationAdd(); } }/*}}}*/ //减法工厂 class SubFactory implements IFactory {/*{{{*/ public function createOperate() { return new OperationSub(); } }/*}}}*/ class Client {/*{{{*/ public static function main($argv) { //简单工厂的switch case 放到了客户端了 $factory = new AddFactory(); $add = $factory->createOperate(); // 设置数字A $add->setNumberA(5); // 设置数字B $add->setNumberB(2); // 运算 echo $add->getResult()."\r\n"; } }/*}}}*/ Client::main($argv); ?>
The above is the detailed content of Power Academy Design Pattern PHP Video Tutorial Sharing. For more information, please follow other related articles on the PHP Chinese website!