[Head First design mode] design mode in Yunnan Rice Noodle Shop-template method mode

Source: Internet
Author: User

Introduction I learned the template method mode when I went to work on the first day and had no tasks. Here I learned how to sell it now. The template method gave me the feeling that I was familiar with it and I always felt that I was using it, at that time, I did not know that it was a template method. It was a sad reminder. On the first day of the year, eating is a big problem. It is hard to find a rice noodle shop with many people. We can only think about the template mode while waiting. Just like before, for food, only by associating knowledge with food can you remember more effectively. The template method mode is one of the most common modes. The template method mode requires the development of writing between the abstract class and the designer of the specific subclass. One designer is responsible for providing the outline and skeleton of an algorithm, and other designers are responsible for providing the logical steps of this algorithm. Inheritance is often used as the main tool for function reuse. At this time, inheritance may be abused. Therefore, we have a design principle: multi-purpose combination, less inheritance, or inheritance, should not be used at all? In fact, data abstraction, inheritance, encapsulation, and polymorphism are the most important features of object-oriented language. Inheritance should not be abused. It does not mean that inheritance should not be used at all. In the GoF book, the vast majority of patterns are converted from inherited implementations to object-based combinations and aggregation. The template method mode is one of the modes rarely implemented by inheritance! And template method mode: encourage proper use of inheritance. This mode can be used to rewrite related classes with the same functions, move reusable general behavior code to the base class, and move the special behavior code to the subclass. Familiar with the template method mode becomes a good place for re-learning and inheritance. Example in the book caffeine beverage Coffee copy code 1 public class Coffee 2 {3 public void PrepareRecipe () 4 {5 BoilWater (); 6 BrewCoffeeGrinds (); 7 PourInCup (); 8 AddSugarAndMilk (); 9} 10 // each method implements a step in the algorithm: boiling water, brewing coffee, pouring coffee into a cup, adding sugar and milk 11 public void BoilWater () 12 {13 14 Console. writeLine ("Boiling water"); 15} 16 public void BrewCoffeeGrinds () 17 {18 Console. writeLine ("Dripping Coffee through filter"); 19} 20 public void PourInCup () 21 {22 Console. writeLine ("Pouring into cup"); 23} 24 public void AddSugarAndMilk () 25 {26 27 Console. writeLine ("Adding Sugar and Milk"); 28} 29} copy code Tea copy code 1 public class Tea 2 {3 public void PrepareRecipe () 4 {5 BoilWater (); 6 // steps 2 and 4 are different from those of coffee. Others are the same as 7 SteepTeaBag (); 8 PourInCup (); 9 AddLemon (); 10} 11 // each method implements one step in the algorithm: boiling water, brewing coffee, pouring coffee into the cup, adding sugar and milk 12 public void BoilWater () 13 {14 15 Console. writeLine ("Boil Ing water "); 16} 17 // tea special 18 public void SteepTeaBag () 19 {20 Console. writeLine ("Steeping the tea"); 21} 22 public void PourInCup () 23 {24 Console. writeLine ("Pouring into cup"); 25} 26 // 27 public void AddLemon () 28 {29 30 Console. writeLine ("Adding Lemon"); 31} 32} copy the code. The first version of the Code was designed to analyze starbaz coffee and tea brewing using the same algorithm: boil water with hot water coffee or tea pour the drink into the cup add appropriate spices abstract PrepareRecipe () 1. The problem we encountered was: tea using SteepTeaBag () and AddLemon () methods, while coffee uses BrewCoffeeGrinds () and AddSugarAndMilk () methods. 2. Both coffee brewing and tea soaking are made in boiling water. We will give it a new method name, such as Brew (). Similarly, whether it is coffee with sugar, milk, or tea with lemon, It is a seasoning, we also give it a new method name AddCondiments (). In this way, the new prepareRecipe () method looks like this: Copy code 1 public void prepareRecipe () 2 {3 BoilWater (); 4 Brew (); 5 PourInCup (); 6 AddCondiments (); 7} copy code 3. CaffeineBeverage (caffeine beverage) superclass: Copy code 1 public abstract class CaffeineBeverage 2 {3 public void PrepareRecipe () 4 {5 BoilWater (); 6 // steps 2 and 4 are generalized to Brew () and AddCondiments (). 7 Brew (); 8 PourInCup (); 9 AddCondiments (); 10} 11 // the two methods are declared as abstract because coffee and tea have different practices and must be implemented by subclass. 12 public abstract void Brew (); 13 public abstract void AddCondiments (); 14 public void BoilWater () 15 {16 Console. writeLine ("Boiling water"); 17} 18 public void PourInCup () 19 {20 Console. writeLine ("Pouring into cup"); 21} 22 23} copy Code 4. Both coffee and tea depend on the super-class (caffeine beverage) Processing brewing method. Copy code 1 public class Coffee: CaffeineBeverage 2 {3 public override void Brew () 4 {5 Console. writeLine ("Dripping Coffee through filter"); 6} 7 8 public override void AddCondiments () 9 {10 Console. writeLine ("Adding Sugar and Milk"); 11} 12} copy code 1 public class Tea: CaffeineBeverage 2 {3 public override void Brew () 4 {5 Console. writeLine ("Steeping the tea"); 6} 7 8 public override void AddCondiments () 9 {10 Console. writeLine ("Adding Lemon"); 11} 12}

Related Article

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.