Home >Backend Development >PHP Tutorial >PHP object-oriented advanced design patterns: Delegation pattern usage examples

PHP object-oriented advanced design patterns: Delegation pattern usage examples

巴扎黑
巴扎黑Original
2017-05-22 14:07:492086browse

One of the most powerful features of object-oriented programming is its dynamic nature. In a world of ever-increasing functionality, hybrid building structures, and evolving standards, dynamic code takes on a whole new meaning. Whether it's a new file storage standard or a streaming standard, a social networking site or something new with an Internet pioneer API, Web programming is constantly changing. Traditional ways of approaching sentencing are no longer effective today when faced with the vast number of options available. By moving intelligent objects into appropriate locations, the delegate design pattern takes away from complex decisions.

What is the delegation pattern?

The delegation design pattern removes decisions and complex functionality from core objects by assigning or delegating to other objects.

Delegation pattern application problems and solutions

Most PHP programmers’ initial contact is almost with the programmatic programming type. This programming style relies heavily on flow control based on conditional statements. Object-oriented programming provides some alternatives to traditional conditional statements, thereby creating a more stateful code flow. One way to achieve this functionality is to create objects based on the delegate design pattern.

The Delegate design pattern strives to remove complexity from core objects. At this point we are not designing objects that rely heavily on performing specific functionality by evaluating conditional statements; objects based on the delegation pattern can delegate decisions to different objects. Delegation can be as simple as using an intermediate object to handle a decision tree, or as complex as using a dynamically instantiated object to provide the desired functionality.

It is very important not to think of the delegate design pattern as a direct competitor to conditional statements. In contrast, the Delegate design pattern helps structure the architecture by invoking the correct functionality without requiring conditional statements. The conditional statement is best located in the actual method, and the processing of the business rules is completed in the method.

An example of using the delegate design pattern is to provide multiple formats for a specific part of data. Suppose there is an archive in an open source code repository. When visitors intend to download a portion of the source code, they can choose between two different file formats. The specified file will be compressed and sent to the browser. In this example, I intend to take files in zip and tgz compressed formats.

When an object contains complex but independent pieces of functionality that must be executed based on decisions, the best practice is to use objects based on the delegate design pattern.


##UML

This UML diagram details a class design using the delegate design pattern.

PHP object-oriented advanced design patterns: Delegation pattern usage examplesPHP object-oriented advanced design patterns: Delegation pattern usage examples

The following is an explanation of the above picture:

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.

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

3. The createDelegateObject() method creates an instance of the delegate object and names the instance according to the delegateType variable. This method then stores the instance internally by assigning it to internalDelegate.

4. The runDelegateAction() method is responsible for running the action() method of the internalDelegate object.

5.MyDelegateObject contains the logic responsible for specific actions. MyObject runs the action() method to implement specific functions.

Application Scenario

Designed a cd class, the class has mp3 playback mode, and mp4 playback mode

Before the improvement, the playback mode of the cd class needed to be used in the instance Class to determine which playback mode to choose

After the improvement, the playback mode is passed into the playList function as a parameter, and the corresponding method to be played can be automatically found.

Code: cd class, before improvement, selecting the playback mode is a painful thing

Usage example:

<?php  
//委托模式-去除核心对象中的判决和复杂的功能性  
//使用委托模式之前,调用cd类,选择cd播放模式是复杂的选择过程  
class cd {  
protected $cdInfo = array();   
 
public function addSong($song) {  
       $this->cdInfo[$song] = $song;  
}  
      
public function playMp3($song) {  
        return $this->cdInfo[$song] . &#39;.mp3&#39;;  
    }  
      
    public function playMp4($song) {  
        return $this->cdInfo[$song] . &#39;.mp4&#39;;  
    }  
}  
$oldCd = new cd;  
$oldCd->addSong("1");  
$oldCd->addSong("2");  
$oldCd->addSong("3");  
$type = &#39;mp3&#39;;  
if ($type == &#39;mp3&#39;) {  
    $oldCd->playMp3();  
} else {  
    $oldCd->playMp4();  
}

Code: improved cd through delegation mode Class

<?php  
//委托模式-去除核心对象中的判决和复杂的功能性  
//改进cd类  
class cdDelegate {  
    protected $cdInfo = 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 = &#39;mp3&#39;;  
$oldCd->play(&#39;mp3&#39;, &#39;1&#39;); //只要传递参数就能知道需要选择何种播放模式

The above is the detailed content of PHP object-oriented advanced design patterns: Delegation pattern usage examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn