PHP Object-oriented Advanced design mode: Delegate Mode usage instance

Source: Internet
Author: User
One of the most powerful features of object-oriented programming is its dynamic nature. Dynamic code has a whole new meaning as the world continues to emerge with more available features, hybrid structures, and standards for sustainable development. Whether it's a new file storage standard or streaming media standard, or a social web site or some new thing on the Internet Pioneer API, Web programming is always going to mutate. The traditional way of handling judgments is no longer valid in the face of a large number of available options today. By moving an intelligent object to the appropriate location, the delegate design pattern is able to move away from complex judgments.

What is a delegate mode?

By assigning or delegating to other objects, the delegate design pattern can remove judgments and complex functionality from the core objects.

Application problems and solutions of delegated mode

Most PHP programmers initially touch almost all programmatic types of programming. This programming style greatly relies on flow control based on conditional statements. Object-oriented programming provides some of the tools that are different from the traditional conditional statements, thus creating more state code flows. One implementation of this feature is to create an object that is based on the design pattern of the delegate.

The delegate design pattern is dedicated to removing complexity from core objects. At this point, we do not design objects that rely heavily on the ability to perform specific functions by evaluating conditional statements, and objects based on the delegate schema can delegate judgments to different objects. Delegates can be as simple as processing a decision tree with an intermediate object, or as complex as providing the desired functionality using a dynamic instantiation object.

It is important not to treat the delegate design pattern as a direct competitor to the conditional statement. Instead, the delegate design pattern helps to make up the architecture by invoking the correct functionality in a way that does not require conditional statements. A conditional statement best resides in the actual method, and the processing of the business rule is done in the method.

A use example of a delegate design pattern is to provide multiple formats for a specific data section. Suppose there is an archive in the open resource code base. When the visitor intends to download some of the source code, they can choose between two different formats of the file. Specifies that the file is compressed and sent to the browser. In this example, I intend to use the zip and tgz compressed file format.

When an object contains complex but separate parts that must be based on the functionality of the execution of a decision, the best practice is to use objects based on the delegate design pattern.


Uml

This UML diagram details a class design that uses a delegate design pattern.

Here is a description of the pair:

1. The base class MyObject knows that objects based on the delegate design pattern will be used. This class contains the private character string delegatetype and the private instance internaldelegate of Mydelegateobject.

The 2.setDelegateType () method receives a parameter named type, which is stored in the Delegatetype string.

The 3.createDelegateObject () method creates an instance of the delegate object and names the instance according to the Delegatetype variable. Then, by assigning the instance to Internaldelegate, this method says it is stored internally.

The 4.runDelegateAction () method is responsible for running the Internaldelegate object's action () method.

5.MyDelegateObject contains the logic that is responsible for a particular action. MyObject Run the Action () method to implement the specific functionality.

Application Scenarios

A CD class was designed with MP3 playback mode and MP4 playback mode in the class.

Before the improvement, using the CD class playback mode, you need in the instantiated class to determine what mode of playback mode to choose

After the improvement, the playback mode as a parameter passed into the playlist function, it will automatically find the corresponding method to play.

Code: CD class, not improved before, choose Play mode is a painful thing

Usage examples:

<?php  //Delegate mode-removes the decision in the core object and complex functionality  //using the delegate mode before calling the CD class, choosing CD playback mode is a complex selection process  class CD {  protected $cdInfo = Array ();    Public Function Addsong ($song) {         $this->cdinfo[$song] = $song;  }        Public Function PlayMp3 ($song) {          return $this->cdinfo[$song]. '. mp3 ';      }            Public Function PlayMp4 ($song) {          return $this->cdinfo[$song]. '. mp4 ';      }  }  $oldCd = new cd;  $oldCd->addsong ("1");  $oldCd->addsong ("2");  $oldCd->addsong ("3");  $type = ' mp3 ';  if ($type = = ' mp3 ') {      $oldCd->playmp3 ();  } else {      $oldCd->playmp4 ();  }

Code: Through the delegate mode, the improved CD class

<?php//Delegate mode-removal of judgments and complex functionality in core objects//improved CD classes class Cddelegate {protected $CDINF             o = Array ();      Public Function Addsong ($song) {$this->cdinfo[$song] = $song;          Public function Play ($type, $song) {$obj = new $type;      return $obj->playlist ($this->cdinfo, $song);      }} class Mp3 {Public function playList ($list) {return $list [$song];      }} class MP4 {Public function playList ($list) {return $list [$song];  }} $newCd = new cd;  $newCd->addsong ("1");  $newCd->addsong ("2");  $newCd->addsong ("3");  $type = ' mp3 '; $oldCd->play (' mp3 ', ' 1 '); Just pass the parameters to know what playback mode to choose 
Related Article

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.