PHP design mode Series-Adapters

Source: Internet
Author: User

    • What is an adapter:

The adapter design pattern simply fits an interface of an object to the interface expected by another object.

    • Design Scenario:
    1. If we primitive have a userinfo class, provide the class of user information, get up early to design this class, only implement a method that GetUserName obtains the user name.
    2. In our Myoldobject class, the user information is obtained from the UserInfo class, and the user name is output
    3. Over time, our old userinfo This class only provides the method to obtain the user name, has not been able to satisfy the demand, we also need to obtain the user's age and so on information.
    4. In order not to change the original userinfo this class, we inherit UserInfo, build a Userinfoadapter class, achieve getage get age such method.
    5. In our mynewobject new class, we instantiate Userinfoadapter and print out the name and age of the user.
    6. Thus, with our expansion, we did not change the original userinfo this class and use the interface of this class, we extend the UserInfo class by the method of adaptation
    • Code: UserInfo class, implementing the GetUserName method
    <? PHP        // an early user class that only implements methods      to get the user name class UserInfo {                publicfunction  getusername () {              return ' Initphp ';          }      }  

Code: Myoldobject class, obtaining information from the UserInfo class, outputting the user name

 <? php  include_once       ("userinfo.php"  class   Myoldobject { public  function   write () { Span style= "color: #800080;"              > $UserInfo  = new   UserInfo;  echo   $UserInfo ->getusername ();  }}   $a  = new   Myoldobject;    $a ->write (); 

Code: Userinfoadapter class, Over time, the project needs are changing, UserInfo class can not meet the requirements, we do the UserInfo class adapter, to meet the needs of new features

<?PHPinclude_once("userinfo.php"); classUserinfoadapterextendsuserinfo{ Public functionGetuserage () {return28; }             Public functionGetUser () {return Array(              ' Username ' =$this->getusername (), ' age ' =$this-getuserage ()); }  } 

Code: MyNewObject class, New function class, need to print out user age and name, UserInfo class can not meet the requirements, need to call Userinfoadapter adapter this class

<? PHP   include_once ("userinfoadapter.php");   class MyNewObject {       publicfunction  write () {          $UserInfoAdapter New Userinfoadapter;           Print_r ($UserInfoAdapter-GetUser ());       }  }   $a New mynewobject;   $a

Transferred from: http://blog.csdn.net/initphp/article/details/7676129

PHP design mode Series-Adapters

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.