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.