Template name:Mechanical energy-saving products production Enterprises Web site weaving Dream TemplateTemplate Description:The page is simple, easy to manage, DEDE5.5 can be used above the core, and test data is included!Template features:Suitable for mechanical and electronic products, electronic science and technology, machinery website Business site use, simpl
Template Method pattern (JAVA) of software design pattern, design pattern java
What is the template method mode?
Defines the skeleton of an algorithm in an operation and delays these steps to the subclass. The template method allows the subclass to redefine certain steps of
their own version, so the original class isDifferent interface subclasses can be combined to invoke different algorithms.Template method pattern is class inheritance, the algorithm framework (STEP) is encapsulated as a virtual base class, and the algorithm framework is not covered, subclasses can only have different implementations of individual steps. The base class can also introduce the hook () function to fine-tune the algorithm framework. Hooks () hook function is very simple, the base cla
Android design mode: Template Method mode; android Design Mode
Template Method mode definition: defines an algorithm skeleton in a method, and delays some steps to the subclass. The template method allows the subclass to keep the algorithm structure unchanged, redefines some
Template design patterns are mainly derived from the life of some things are template can be followed. Give two examples of life, such as tea and coffee, and take a look.Tea has the following four steps: 1, boiled water; 2 Put the tea in the Water Cup; 3, pour boiling water; 4, add sugar.Brewed coffee also has the following four steps: 1, boil water; 2 Put the co
What is a template method pattern?Defining the skeleton of an algorithm in an operation, and deferring these steps into subclasses, the template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.Good abstract concept Ah, genteel things are not pleasing, below I use a common example of life to illustrate itis a
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
slice");
}
Brewing Drinks
public static void Main (string[] args) {
System.out.println ("Bubble coffee");
Beverage coffee = new coffee ();
Coffee.preparerecipe ();
System.out.println ("Bubble lemon green Tea");
Beverage tea = new Tea ();
Tea.preparerecipe ();
}
Output
Brew coffee, boil water,
add coffee,
pour the hot drinks into the cup,
add sugar,
lemon green tea,
boil
the tea,
pour the hot dr
Template Method pattern of software design pattern (JAVA)Everyone is familiar with it. There is a thing that can be seen everywhere in major malls. Here is an example. For example, if we want to drink a certain kind of beverage, the beverage will automatically come out by pressing the corresponding beverage category. Here we can abstract and imagine the beverage
Mode motiveTemplate method pattern is a basic technique based on inheritance, and the structure and usage of template method pattern are also the core of object-oriented design. In the template method pattern, you can put the same code in the parent class and put different method implementations in different subclasses.In the
, the request will follow the prototype chain to find beverage "parent class" corresponding Beverage.prototype.brew method, and the Beverage.prototype.brew method so far is an empty method, which is clearly not in line with what we need.There are no static language checks in JavaScript. We do not get any form of warning when writing code, and it is dangerous to rely entirely on the programmer's memory and self-consciousness, especially when we are using the
Introduction to Template Method Patterns Template method, as the name implies, is the general process of doing some tasks. If there are many self-introduction templates on the Web, the recommendation template, that is, the beginning and end of the content may be similar, and in the middle of the need for customers to modify the use.
Template method Pattern Definition: Define the skeleton of an algorithm in a method, and defer some steps into the subclass, the template method allows subclasses to redefine some of the steps in the algorithm without altering the algorithm structure.Design principles: Do not call us, I will call you, (like headhunters and job seekers, job seekers do not need to find a headhunter, headhunting as long as the
. We continue to supplement the various subsequent additions based on the original code, and our code becomes unsightly under the new if statement line. Of course, as programmers, we don't have much of a say in demand, and there's nothing we can do about it. But we can do our best to design the architecture of the program, let us write the code more extensibility, so that we can deal with a variety of requirements changes.
Below you will use the
In an abstract class, the way in which methods are executed is publicly defined, subclasses can be implemented as required, but calls are made in the manner defined in the abstract class, typical applications such as banking processes, and brewing beverage processes. The following is a simple example, with boiling water brewing beverage, divided into four steps: boil, brew beverage, pour the drink into the cup, add seasoning.1.
different, but can be generalized to two the same behavior.Public abstract class Drink { //Water heaters Public void boilwater( ) { } //Pour into a water cup Public void incup( ) { } //Brew, abstract Public abstract void brewing( ); //plus materials, abstract Public abstract void xxincup( ); } Public class coffe : Drink {Public override void brewing( ) { } Public override void xxincup( )
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 o
, so just rewrite Step2 ~ STEP4,STEP1 can continue to use. First design a drinkmachine prototype, defined the production process and STEP1, because Step2 ~ step4 each have differences, it is left in the inheritance of Drinkmachine class to rewrite, this is template method patterns.
Let's take a look at this architecture, and if a new drink is added after the
1. Template Method mode 1.1 object-oriented implementation of template method patternTake tea and coffee for example, can be organized into the following four steps
Boil the water
Brew a drink in boiling water
Pour the drink into the cup
Add seasoning
First, create an abstract parent class to
() { } /** * Subclass cannot access the method * /Private Function buyaflight () { echo "Buying a flight\n"; } /** * This is also a final method * /FINAL protected function Takeplane () { echo "taking the plane\n"; }}
beachjourney.php
cityjourney.php
4. Test code tests/journeytest.php
Expectoutputregex (' #sun-bathing# '); $journey->takeatrip (); Public Function testcity () {$journey = new Templatemethod\c
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.