20. StarCraft PHP Design mode--Adapter mode

Source: Internet
Author: User

Preface
==============================================================================
This PHP design mode album from the blog (jymoz.com), now has no access, this series of articles I have been looking for a long time to find the complete, thanks to the author Jymoz's Hard Pay Oh!

This address:http://www.cnblogs.com/davidhhuan/p/4248208.html
==============================================================================


Many of the star's classes have at least one special skill. And some classes have the same skills, for example, the Zerg forces will recover their blood.
If you follow the general line of thinking, the operation and control of skills as a method, placed in the definition of each class to implement, the code will be repeated, it is not easy to modify.
Then we will consider inheriting, for example, we can design a base class of Zerg, which has the method of recovering blood after injury.
When designing a thorn snake (Hydralisk, the mouth sailor), we can let the snake class inherit the Zerg base class.
But the Thorn snake can be developed drilling ground, and drilling is not the unique function of the Thorn snake, is the Zerg ground forces have characteristics, we also want to drill ground as a common base class.
The problem comes out, we can't let the Thorn Snake class inherit two classes at the same time, this is not allowed by PHP.

problem to be solved: how to mix and reuse two classes,

idea: inherit a class, create the object of one of the classes as a property, and then call the method of the second class through this property.

Example of adapter (Adapter) mode:

<?PHP//Zerg base class    classZerg {//Blood         Public $blood; //ways to restore blood         Public functionRestoreblood () {//automatic gradual recovery of the blood of the classes        }    }    //the class of the drill ground    classBurrow {//the method of drilling the ground         Public functionburrowoperation () {//the movement of the ground, invisibility, etc.            Echo' I drilled the ground '; }    }    //the class of the Thorn snake    classHydraliskextendsZerg {//To hold a property to a drill ground object         Public $burrow; //constructor, because PHP does not allow the default values to take the object, so the initialization is assigned to the $burrow         Public function__construct () {$this->burrow=NewBurrow (); }        //the method of drilling the ground         Public functionburrowoperation () {//call the object where the drill ground property is stored, using the method of the drill ground class            $this->burrow->burrowoperation (); }    }    //make a thorn snake    $h 1=NewHydralisk (); //let him drill the ground    $h 1-burrowoperation ();?>

Usage Summary: The adapter mode allows a class to use the functionality of two basic classes at the same time, jumping out of simple inheritance restrictions. Effective reuse of multiple categories.

Implementation Summary: let the new class inherit a base class, and then use the properties of the new class to hold objects of other classes, through which the methods of other classes are invoked.

Related articles:

1. StarCraft PHP Object-oriented (i)

2. StarCraft PHP Object-oriented (ii)

3. StarCraft PHP design mode-Simple Factory mode

4. StarCraft PHP Design mode-factory method mode

5. StarCraft PHP design mode-Abstract Factory mode

6. StarCraft PHP design mode-builder mode

7. The PHP design mode of StarCraft--The mediator mode

8. StarCraft PHP design mode--Enjoy meta mode

9. StarCraft PHP Design mode--proxy mode

10. StarCraft PHP design mode-prototype mode

11. The PHP design mode of StarCraft--Memo mode

12. StarCraft PHP design mode-template mode

13. StarCraft PHP design mode-positive mode

14. StarCraft PHP design mode-state mode

15. StarCraft PHP design mode--strategy mode

16. StarCraft PHP Design mode--combination mode

17. StarCraft PHP Design mode--responsibility chain mode

18. StarCraft PHP design mode-Observer mode

19. The PHP design mode of StarCraft--iterator mode

? 20. StarCraft PHP design mode-Adapter mode

20. StarCraft PHP Design mode--Adapter mode

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.