7. The PHP design mode of StarCraft--The mediator 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/4248183.html
==============================================================================


The interstellar upgrade system is more balanced, but because many of the classes and buildings need to be related to the construction of science and technology, so the relationship is more complex.
For example, when a science station is built, all airports can build tech balls, but once a science station is destroyed, it is necessary to see if there is a science station, otherwise you have to let all the airport can not build technology ball.
We can use the last-mentioned observer pattern to solve the problem, but because of the upgrade in the interstellar more relevant, it seems to be more troublesome.
In essence, any upgrade is generally as long as it knows whether a certain building exists or not, so we don't have to make them much more connected, set up a mediator on the line.
It is as if we do not buy anything, to the supermarket can be, and manufacturers as long as the supermarket contact, do not have to contact each of our customers directly.

problem to be solved: don't let the buildings connect with each other and reduce the complexity.

idea: set up intermediaries, every time encountered manufacturing technology-related things, ask the intermediary.

Example of a broker (mediator) Pattern:

<?PHP//Intermediary Person    classMediator {//storing the number of science and technology buildings, in order to simply explain, with static properties, can actually let the individual objects to handle         Public Static $techBuilding; //according to the name of the building represented by the parameter $techbuildingname, return the existence of the corresponding science and technology buildings, in order to explain briefly, with static properties         Public Static functionIstechallow ($techBuildingName)        {            //returns True if the number of scientific buildings is greater than 0, otherwise false            returnSelf::$techBuilding[$techBuildingName]>0; }        //Once the technology building has been built or destroyed, call this method, the parameter $techbuildingname represents the building name, $add to Boolean, true to increase (build), false to decrease (destroy)         Public Static functionChangetech ($techBuildingName,$add)        {            //Construction            if($add)            {                //Increase QuantitySelf::$techBuilding[$techBuildingName]++; }            Else            {                //Reduce QuantitySelf::$techBuilding[$techBuildingName]--; }        }    }    //Science and Technology station category    classsciencefacility {//Construction Method         Public function__construct () {Mediator:: Changetech (' sciencefacility ',true); }        //destructor Method         Public function__destruct () {Mediator:: Changetech (' sciencefacility ',false); }    }    //Airport Class    classStarport {//the way to make tech balls         Public functionCreatesciencevessel () {//ask the intermediary to decide if you can make a tech ball            EchoMediator::istechallow (' sciencefacility ')? ' Can make tech balls ': ' Can't make tech balls '; }    }    //build a technology station    $scienceFacility 1=Newsciencefacility (); //rebuilding a technology station    $scienceFacility 2=Newsciencefacility (); //build an airfield .    $starport=NewStarport (); //Building Tech balls, the result is being able to    $starport-Createsciencevessel (); //a technology station was destroyed .    unset($scienceFacility 1); //The technology ball was built, and the result was that there was a technology station    $starport-Createsciencevessel (); //Another technology station was destroyed .    unset($scienceFacility 2); //The technology ball was built, and the result was No.    $starport-Createsciencevessel ();?>

Summary of Use: the mediator pattern reduces the communication of individual objects and avoids the association of code.

Implementation Summary: the intermediary model is more flexible, generally as long as there are intermediaries and need to be coordinated class, the specific design to see the problems encountered.

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

7. The PHP design mode of StarCraft--The mediator 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.