PHP design pattern series-delegated pattern (Delegation)

Source: Internet
Author: User
PHP design pattern series-delegated pattern (Delegation) 1. Pattern Definition

Delegation is a method to extend and reuse the functions of a class. The method is to write an additional class to provide additional functions, and use the original class instance to provide the original functions.

Assume that we have a TeamLead class that delegates its established tasks to a JuniorDeveloper associated with the auxiliary object: Originally, TeamLead processes the writeCode method, and Usage calls this method of TeamLead, however, TeamLead now delegates the writeCode implementation to the writeBadCode implementation of JuniorDeveloper, but Usage does not perceive that the writeBadCode method is being executed.

2. UML class diagram

3. Sample code

Usage. php

 WriteCode ();

TeamLead. php

 Slave = $ junior;}/*** TeamLead coffee, JuniorDeveloper works * @ return mixed */public function writeCode () {return $ this-> slave-> writeBadCode ();}}

JuniorDeveloper. php

 4. test code

Tests/DelegationTest. php

  AssertEquals ($ junior-> writeBadCode (), $ teamLead-> writeCode ());}}

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.