The template method pattern of "Head First design mode"

Source: Internet
Author: User
Tags comparable

Now there are two people, one Love coffee, one love tea, let us use the code to create coffee and tea:

Coffee type:

Tea Type:

It's easy to see that the code for coffee and tea is surprisingly similar.

After reading the design pattern, you should have already consciously extracted the same parts. The Pourincup and Boilwater methods are the same, so consider incorporating them into a common class, which we can design:

Caffeinebeverage is a public class, coffee tea is its subclass, inheriting the same method.

Wait, is that the same part? No

Brewcoffeegrinds () and Steepteabag () two methods, one is brewing, one is soaking, actually similar, is not also considered to extract it? Is it the same for Addsugerandmilk () (add sugar) and Addlemon () (add lemon)?

We use Brew () to denote brewcoffeegrinds () and Steepteabag (), use addcondiments () to represent Addsugerandmilk () (add sugar) and Addlemon ().

The code for the public class can be:

Now the specific coffee tea class only need to achieve bubble and add seasoning two ways to do it:

Compare the first code before the coffee tea is not saved a lot of code? It's easier to have a similar drink in the future, to prevent a lot of repetitive code coming up.

In fact, the above is using template method design mode

In the example above, the template method is Preparereceipe ().

The Caffeinebeverage class dominates the algorithm flow of coffee and tea, and the reuse of the code is almost maximized, adding that the new beverage type does not need to add too much code, just to implement a different method than other drinks.

In general, it can be said that Caffeinebeverage provides a framework that focuses on the algorithm itself, which provides a complete implementation by subclasses.

Let's take a look at the official definition:

The template method defines the skeleton of an algorithm in a method, and delays some steps into subclasses. The template method allows subclasses to redefine some of the steps in the algorithm without changing the structure of the algorithm.

Look at the class diagram:

The template Method Templatemethod () calls Primitiveoperation () 1 and primitiveoperation () 2, which does not care about the implementation of the two methods, thus implementing the decoupling of the template method and some specific subclass operations.

In the Java API, a typical example of using template method patterns is sort. If we sort the number of shapes or floating-point data, we can sort them from small to large or from big to small, if you want to sort a group of objects, you might ask how the objects compare size.

Yes, there is no direct comparison between objects, so Java provides the comparable interface, there is a CompareTo method, each class needs to be sorted to implement this interface, in the CompareTo method to implement specific sorting methods, such as sorting a group of cats, You can sort by the weight of the cat, or you can use your height. This creates a class that can be sorted, and then passes an array of that class object to the static method sort of the array class, which can be sorted according to the sorting criteria established by the CompareTo method. Now introduce a bunch of duck sort examples:

The Duck class is created to implement the CompareTo method of the comparable interface, allowing it to be sortable in a way that is sorted by weight size.

Client program:

The sort method of the array is implemented:

The comparable interface is similar to the previous Caffeinebeverage functionality, deferring the specific sort method CompareTo method to the subclass implementation, because different subclass sorting criteria are definitely different. In this way, the sort method of the array can be used to sort any class object that implements comparable.

Do you think the template method pattern is similar to the previous one? Yes, it is the factory method pattern, which can be said, the factory method mode is a special template method pattern.

The template method pattern of "Head First design 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.