Design Pattern (ix): Learn "template method pattern" from vinegar-shredded potatoes and fried balsam pear

Source: Internet
Author: User
Tags pear

Today is five. Four Youth Day, I wish you a happy holiday. Look at today this title has appetite, summer, vinegar and shredded potatoes and fried balsam pear suitable for summer to eat, good eat fire. These two dishes should be eaten by most people, especially vinegar and shredded potatoes, as one of the masterpieces of "Shandong Cuisine" is known to the public, vinegar shredded potatoes, good eat fire. Stir fry bitter gourd this dish is good, is also the summer essential fine vegetable, its effect here does not do too much to repeat. To start with, the last blog we learned from the "Little brother " in the "appearance mode", we also called the "appearance mode" as "younger brother mode." Today we are going to learn about the theme of our blog today, "template method pattern", from the process of making vinegar-shredded potatoes and fried balsam pear.

When it comes to template approach patterns, you should be familiar with template method patterns if you've seen a blog post about refactoring. In the third part of the blog " Code Refactoring (v): Inheritance relationship refactoring rules ", the "template method pattern" is used to reconstruct, in the refactoring rule we call it "Form template Methods" ( constructor function ", this template function is the template method in our blog. Today we will look at the "template method mode" from another angle, and learn "template method mode" from the production examples of "vinegar-shredded potato" and "Fried balsam pear". In this blog, you are not only a programer, but also a cook.

The usual, at the beginning of the blog, we first give the definition of "template method pattern". If you are small white, the definition does not understand the matter, because the definition is generally more difficult to understand. You can read the specific examples below and look back at the definition. The template method is defined as follows:

Template Method Pattern : A skeleton of an algorithm is defined in a method, and some steps are deferred to subclasses. The template method allows subclasses to redefine some of the steps in the algorithm without changing the structure of the algorithm.

First, start cooking

Next we are going to start making our vinegar shredded potatoes and fried balsam pear Two courses, of course, in the first part of this blog we will not use our template method to stir fry. In this section we give a general approach to cooking, and then we will analyze it and eventually use our "template method pattern" to stir fry. Of course, we are not really cooking with the pot, but with our code to stir fry, walk.

1. Stir-fry a vinegar-shredded potato

Next we want to fry a vinegar and shredded potatoes, vinegar and potato silk delicious, and easy to do. Or that sentence, vinegar and shredded potatoes, good eat fire. Because fried "vinegar shredded potato" We are in a class, so we do not draw class diagram, and so on after the use of template method mode, the corresponding class diagram is given. Below the Fryshreddedpotatoes class is we do "vinegar shredded potatoes" class. A series of steps are given for making vinegar and shredded potatoes, and the series of functions are combined in the Fryshreddedpotatoes () function.

The above class, we can instantiate this class and call Fryshreddedpotatoes () to fry a plate of vinegar and shredded potatoes. Below we will instantiate the fryshreddedpotatoes, then fry a plate of vinegar and shredded potatoes.

2, to a plate "fried bitter gourd"

The potatoes were fried and they were out of the pot. Next we will have a plate of "fried bitter gourd", of course, "Fried bitter gourd" and the above "vinegar shredded potatoes" poor, but some of the steps are different. Of course, you can not put vinegar when you fry bitter gourd, there are fried balsam pear when you put the bitter gourd slices instead of potato silk, these are different. Of course, the names of the two dishes are not the same. Below this class is our "Fried bitter gourd" class, you can instantiate the class and then to do a dish belongs to your fried balsam pear. On the whole, the code below is similar to the steps in the above-mentioned class, but the specifics, and the methods that some of the steps call, are different. Below is our class of fried balsam pear, as shown below:

Then is to instantiate the above "Fried bitter gourd" class, and then to a plate of fried balsam pear. Below is the object of the class we created above, and then go to call the method of fried balsam pear. Below is the method of invocation and output, through the process below, we can pan out a plate of fried balsam pear, as follows:

Second, use "template method" to stir Fry

In the above two kinds of cooking methods, we are not difficult to see fried vinegar and shredded potatoes and fried balsam pear in the class there are many duplicate code. In the blog of our refactoring series we have already mentioned the need to remember duplicate code, so we have to refactor the code for the two classes above, and refactoring is done by using the template method pattern for refactoring. It is essentially separating the changed parts from the immutable ones, and the parts that change in that instance are the specifics of the steps, and the steps and parts of the steps are the same.

The whole process of cooking is constant in the cooking example, and some steps in the process are constant. The change is the name of the method of the report of the dish is different, and then the fried vegetables are different, a fried potato is a fried is bitter gourd, the last is added to different spices. Such an analysis, we can put the immutable method into the parent class, the cooking of the unchanged steps encapsulated as a "template method", some of the exact steps (such as oil, etc.) can be placed in the extension of the implementation. This part of the code structure is slightly more complex, so we will give the corresponding class diagram.

1. The reconstructed class diagram is as follows (using "Template method Mode")

First we will give the reconstructed class diagram, and the class diagram after refactoring using the template method pattern is shown below. Fryvegetablestype is the cooking agreement we created, which defines all the methods of "vinegar shredded potatoes" and "Fried balsam pear", but we have renamed the two different methods to unify them. Because the two different methods to achieve the general consistency of things, such as the previous Qing-fried balsam pear in the method of Putbittergourd (), we renamed the "Put Vegetables" putvegetables () method. Putvegetables () method for "vinegar shredded potato silk" is also possible, because Putvegetables () in the "Fried bitter gourd" in the place is bitter gourd, in the fry potato silk is put potato silk.

In the extension of the Fryvegetablestype protocol, we give the same default implementation, such as the template Method (Fry ()), the Oil Discharge (Putsomeoil ()), the chopped green onion (putsomegreenonion ()), the Pan (Outofthepan () ) and other methods. The way to extend the two dishes is constant. And our fried shredded potatoes and fried balsam pear in the implementation of the method is the two different places, the class diagram is as follows:

2. Cooking interface and interface extension code implementation

According to the class diagram above, we can give the cooking interface and the code implementation of the interface extension. The next thing to do is to extract the invariant parts into the extension of the interface and the interface, and below is the interface Fryvegetablestype of the stir-fry and the extension corresponding to the interface. The Fryvegetablestype protocol gives the template method Fry (), as well as the steps of cooking (ie, the algorithm of cooking). In the extension of this interface, the default implementation invocation of the template Method Fry () and these steps, and gives the implementation of some invariant steps. The specific code looks like this:

3. The concrete realization of "vinegar-shredded potato" and "qing-fried Balsam Pear"

The code snippet below is the code implementation in the template method pattern for vinegar-shredded potatoes and fried balsam pear. From the code below, it is easy to see that both follow the Fryvegetablestype protocol and have the default extension for that protocol. We know that the code in the default extension is similar to the default implementation of an abstract class, which is common to subclasses, so there are only a few different steps for the two classes below. For example, in vinegar shredded potatoes put in the seasoning is salt and vinegar, and in the fried bitter gourd in the seasoning is salt. Of course, the other two methods of implementation in two subclasses are also different. Regardless of how the subclass gives the default implementation steps, the template method we give in the default extension is constant, that is, the specific steps of cooking are constant. This is the template method pattern, the template method does not care about the specifics of each step, only the order in which the steps are executed, which is what the so-called template approach is the encapsulation of the algorithm, not the specifics of the calculation.

One of the obvious benefits of using a template approach is to reduce the code redundancy, separating the changed parts from the unchanging ones. In this example, the invariant part is placed in the default extension of the protocol, and the changed part is placed in the subclass. This is the template method pattern.

4. Test cases for "template method mode"

Next we will test the above example, below is the test case of our template method, in fact, the test case below is similar to the test case without using the template method, but the method of stir-frying is different. Although there are some differences in the methods that are called, the difference here is only the difference in the function name, and the function does not change anything. Below is the test case of our refactored code and the result of the run. As you see from the results, the output of the test cases that we did not use the template method with are consistent. This is what we've often mentioned in the Refactoring series blog to change the structure inside the code without changing the interface that the code calls externally.

This blog is similar to the section on "Building template Methods" in our class refactoring, which is the template method pattern described in this article. And in the front we use the template method pattern from the perspective of refactoring, and today's blog theme is not refactoring but our "template method pattern". Due to space limitations, our blog today will be here first, and we will continue to update the design patterns of other swift versions later.

The code in today's blog is shared on GitHub at:https://github.com/lizelu/DesignPatterns-Swift

Design Pattern (ix): Learn "template method pattern" from vinegar-shredded potatoes and fried balsam pear

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.