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 codeTests/DelegationTest. php
AssertEquals ($ junior-> writeBadCode (), $ teamLead-> writeCode ());}}