zoukankan      html  css  js  c++  java
  • PHP设计模式适配器模式

    适配器设计模式只是将某个对象的接口适配为另一个对象所期望的接口。

    适配器设计模式的目标是有助于面向对象的代码,该模式可以为对象接口创建回话。

    通过实例说明:

     1 /*
     2      在项目最初的代码库中,名为errorObject的对象能够处理所有错误的消息和代码。直接将消息输入至控制台:
     3  */
     4 
     5 class errorObject{
     6     private $__error;
     7 
     8     pubic function __construct($error){
     9         $this->__error = $error;
    10     }
    11 
    12     public function getError(){
    13         return $this->__error;
    14     }
    15 }
    16 
    17 class logToConsole{
    18     private $__errorObject;
    19 
    20     public function __construct($errorObject){
    21         $this->__errorObject = $errorObject;
    22     }
    23 
    24     public function write(){
    25         fwrite(STDERR,$this->__errorObject->getError());
    26     }
    27 }
    28 
    29 /*
    30      这样在使用时只需要如下代码:
    31  */
    32 
    33 $error = new errorObject("404:Not Found");
    34 
    35 $log = new logToConsole($error);
    36 
    37 $log->write();
    38 
    39 /*
    40      这时项目中加入了新的网络管理员,需要将错误记录至一个多列CSV文件,要求第一行是错误代码,第二行是错误文本。
    41  */
    42 class logToCSV{
    43     const CSV_LOCATION = 'log.csv';
    44 
    45     private $__errorObject;
    46 
    47     public function __construct($errorObject){
    48         $this->__errorObject = $errorObject;
    49     }
    50 
    51     public function write(){
    52         $line = $this->__errorObject->getErrorNumber();
    53         $line .= ',';
    54         $line .= $this->__errorObject->getErrorText();
    55         $line .="\n";
    56 
    57         file_put_contents(self::CSV_LOCATION,$line,FILE_APPEND);
    58     }
    59 }
    60 
    61 /*
    62      针对这个问题可以1.更改现有代码库的errorObject类;2.创建一个Adapter对象。考虑到保持这些公共接口标准型的需求,创建一个Adapter对象是最佳的选择。
    63  */
    64 
    65 class logToCSVAdapter extends errorObject
    66 {
    67     private $__errorNumber,$__errorText;
    68 
    69     public function __construct($error){
    70         parent::__construct($error);
    71 
    72         $parts = explode(':',$this->getError());
    73 
    74         $this->__errorNumber = $parts[0];
    75 
    76         $this->__errorText = $parts[1];
    77     }    
    78 
    79     public function getErrorNumber(){
    80         return $this->__errorNumber;
    81     }
    82 
    83     public function getErrorText(){
    84         return $this->__errorText;
    85     }
    86 }
    87 
    88 /*
    89      这样,在使用时可以这样调用:
    90  */
    91 
    92 $error = new logToCSVAdater("404:Not Found");
    93 
    94 $log = new logToCSV($error);
    95 
    96 $log->write();

      在需要转化一个对象的接口用于另一个对象时,实现Adapter对象不仅是最佳的做法,而且也能减少很多麻烦。

  • 相关阅读:
    75. Sort Colors
    101. Symmetric Tree
    121. Best Time to Buy and Sell Stock
    136. Single Number
    104. Maximum Depth of Binary Tree
    70. Climbing Stairs
    64. Minimum Path Sum
    62. Unique Paths
    css知识点3
    css知识点2
  • 原文地址:https://www.cnblogs.com/fanchangfa/p/2871966.html
Copyright © 2011-2022 走看看