PHP closure principle (OCP) Use case analysis

Source: Internet
Author: User
This time to bring you the PHP closure principle (OCP) Use case analysis, the PHP closure principle (OCP) to use the attention of what, the following is the actual case, together to see.

1. What is "open-closed"

With the increasing of the scale of software system, the complexity of maintenance and modification of software system continues to improve, this predicament has prompted the French engineering academician Bertrand Meyer to put forward the principle of "open-closed" (Open-close PRINCIPLE,OCP) in 1998. The basic idea of this principle is:

The behavior of the open for Extendtion module must be open, extensible, and not rigid.

Close (Closed for modification) should not affect or massively affect existing modules when extending the functionality of the module.

In other words, developers are required to extend the software functionality of the application without modifying the existing code (source code or binary code) of the system. In a nutshell, a module should be developed in terms of extensibility and should be closed in terms of change.

In life, the easiest example is the computer, we can easily expand the function of the computer, but only through the interface to connect to different devices.

Open-closed can improve the scalability and maintainability of the system, but this is also relative, for a computer can not be completely open, some devices and functions must be stable to reduce maintenance difficulties. To implement a new feature, you'll have to upgrade your hardware or change to a higher-performance PC. Taking the multimedia player software in computer as an example, as a player, it should have some basic and general functions, such as opening multimedia files, stopping playing, fast entering, volume adjusting and so on. But no matter what the player, no matter what the playback platform, following the principle of the design of the player should have a unified style and operating habits, regardless of which one, should ensure that the author can quickly get started.

Take the player as an example, first define an abstract interface, as shown in the code below.

Interface process{public  function process ();}

This interface is then extended to enable decoding and output functions, as shown below

Class Playerencode implements process{public  function process ()  {    echo "encode", Php_eol;  }} Class Playeroutput implements process{public  function process ()  {    echo "ouput", Php_eol;  }}

For the various functions of the player, here is open, as long as you follow the Convention, the implementation of the process interface, you can add new functional modules to the player. Only the decoding and output modules are implemented here, and more new modules can be added according to the requirements.

Next define the player's thread high dispatch manager, once the player receives the notification (can be an external click behavior, can also be an internal notify behavior), will callback the actual thread processing, the code is as follows

Class playprocess{  private $message = null;  Public Function construct () {} public  function callback (event $event)  {    $this->message = $event Click ();    if ($this->message instanceof process)    {      $this->message->process ();}}  }

The concrete product comes out, here defines a MP4 class, this class is relatively closed, which defines the processing logic of the event, the code is as follows

Class Mp4{Public  function work ()  {    $playProcess = new playprocess ();    $playProcess->callback (new event (' encode '));    $playProcess->callback (new event (' output '));}  }

Finally, the processing class for event sorting, which is responsible for sorting events, judging the user or internal behavior to produce the correct "thread" for the player's built-in Pedigree Manager Scheduler, the code is as follows

Class event{  private $m;  Public function construct ($me)  {    $this->m = $me;  }  Public function Click ()  {    switch ($this->m)    {case      ' encode ';        return new Playerencode ();        break;      Case ' output ':        return new Playeroutput ();        Break;}}}  

The last code to run

$MP 4 = new mp4; $mp 4->work ();

The results of the operation are as follows:

Encode ouput

2. How to comply with the open-closed principle

The core of achieving open-closed is to abstract programming, not specific programming, because the abstraction is relatively stable. Let the class depend on the fixed abstraction, such modification is closed, but through the object-oriented inheritance and the polymorphism mechanism, can realize the inheritance of the abstract body, by covering its methods to change the inherent behavior, to implement a new extension method, so for the extension is open.

1) Apply the idea of "abstraction" and "encapsulation" in the design aspect.

On the one hand, in the software system to find a variety of possible "variable factors" and to encapsulate them.

On the other hand, a variable factor should not be scattered in multiple different code modules, but should be encapsulated into an object.

2) Application of interface-oriented programming in the implementation of system functional programming.

When the requirements change, the interface can be provided with a new implementation class in order to adapt to the change.

Interface-oriented programming requires a functional class to implement an interface, and an object is declared as an interface type. In the design mode, the decoration mode is more obviously used in the OCP.

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Richter Replacement Case detailed

bootstrap+php implementation of multi-image upload steps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.