StarCraft php template mode-PHP development

Source: Internet
Author: User
Tags php template
The zerg troops in the interstellar industry have a special evolutionary weapon, namely, the Flying Dragon. The flying dragon can become an Air Guard (tiancrab) or a swallowed-up (to the air ). In addition, there is also the ability to evolve into a ground thorn. The three changes are similar: the changed original troops disappear, produce an egg or cocoon, hatch for a while, the eggs disappear, and new troops are generated. The zerg troops in the interstellar industry have a special evolutionary weapon, namely, the Flying Dragon. The flying dragon can become an Air Guard (tiancrab) or a swallowed-up (to the air ). In addition, there is also the ability to evolve into a ground thorn.


The three changes are similar: the changed original troops disappear, produce an egg or cocoon, hatch for a while, the eggs disappear, and new troops are generated.


If we develop these three evolution independently, We will generate duplicate code, and the redundancy will increase. Therefore, we need to try to reduce unnecessary code.


The problem to be solved: the same steps are required, but the details of each step are different.


Idea: to build an evolutionary Engineering Framework, we only need to control details.



Template mode example:

 BecomeEgg ($ troop); // wait for the egg to hatch. The parameter is eggs $ this-> waitEgg ($ egg ); // generate the new troops after hatching return $ this-> becomeNew ($ egg);} // The following three Abstract METHODS, abstract public function becomeEgg ($ troop); abstract public function waitEgg ($ egg); abstract public function becomeNew ($ egg );}

Here, we use the evolution class of the Sky Guard (tiancrab) to demonstrate the solution of the ground thorn and so on.

// The evolution class of tiancrab inherits the abstract evolution class GuardianEvolution extends evolution {// generates an egg public function becomeEgg ($ troop) {// destroys the Flying Dragon, return code of an egg object} // wait for the egg to hatch the public function waitEgg ($ troop) {// wait several dozen seconds for the code} // generate the new force public function becomeNew ($ troop) {// destroy the egg, returns a day crab} // creates a new day crab evolutionary object $ e1 = new GuardianEvolution (); // calls the evolution framework function of the parent class, automatically complete three steps $ e1-> process ($ sds);?>

Summary: The template mode can automate a series of steps while meeting different details.


Implementation Summary: an abstract class is required to include a framework function, so that a specific subclass can inherit it and implement all the steps. You only need to call the framework function automatically.

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.