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
Description: Defines an algorithm skeleton in an operation, delaying some of the steps into subclasses. Prepare a draw template, implement part of the logic, and then declare some common abstract methods that require different implementations for subclasses.
Scenario: For multiple businesses, many functions in their business are similar, and some functions need to be implemented on their own. This allows us to use
, subclass selection implementation};/*Brew coffee based on the template method, only virtual functions are realized*/classTea: PublicCaffeinebeverage{ Public:VirtualvoidBrew (){std::cout"Dripping coffee through filter."}VirtualvoidAddcondiments (){std::cout"Adding Suger and milk."}};/*make tea based on template method
Template mode: Defines an algorithm skeleton in an operation, and delays some steps into subclasses.According to the example of "Headfirst design mode", the algorithm Framework (flow) for brewing and brewing coffee is the same, but some algorithms are implemented differently and some are the same.We can encapsulate the common algorithm framework as a virtual base
abstract methods, let the subclass to implement, so the subclass usually has more free space, and the template method is the parent class defines the algorithm framework, subclasses to implement the parent class of the abstract method, so the role of the subclass can affect the parent class. examples of application of template methods Background Introduction Suppose you want to make some beverage product
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 production process in the machine (here is ju
("input error, no seasoning \ n by default ");Return false;}}Void Tea: BREW (){Printf ("Tea \ n soaked in boiling water ");}Void Tea: addcondiments (){Printf ("add Lemon \ n ");}Bool Tea: mermerwantscondiments (){Char zanswer;Printf ("Do you need lemons in your tea :");Fflush (stdin );Zanswer = getchar ();Zanswer = toupper (zanswer );If ('y' = zanswer){Return true;}Else if ('n' = zanswer){Return false;}Else{Printf ("input error, no seasoning \ n by default ");Return false;}}Int main (){
("Pour into cup");}//Hook method that determines whether certain algorithm steps are hooked up in the algorithm PublicBooleanCustomwantscondiments () {return true;}//Special Operations, specifically implemented in subclasses PublicAbstractvoidBrew ();//Special Operations, specifically implemented in subclasses PublicAbstractvoidAddcondiments ();}//Specific ClassclassTeaextendsBeverage {@Override PublicvoidBrew () {System.out.println ("brewing tea");}@Override PublicvoidAddcondiments () {System
JavaScript: Template Method of Design PatternIntroduction
The template method (TemplateMethod) defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
Th
Introduction
The template method (templatemethod) definesAlgorithmAnd delay some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
The template method isCodeThe basic reuse technology is particularly important in the class library because it extracts the publ
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 drink machine, everyone is
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
I. Definition
Template method is based on the design pattern of inheritance, which can improve the scalability of the system. Abstract parent class, subclass in JavaThe template method has two parts structure, the first part is abstract parent class, the second part is the concrete implementation subclass.
Second, the example
From January 1 to 31st, foreign well-known design website ThemeForest held a fierce website template design competition. Many designers participated in the competition and submitted many excellent works. The winner's list was selected and announced in early February, with a total of 10 designers winning the game.
This list shows the winning works of the competit
Template methodA template method is a method that defines the steps of an algorithm and allows subclasses to provide implementations for one or more steps.Examples of brewing coffee and tea in this example illustrate:Explain the steps of brewing coffee and tea, you can see the steps of brewing
the decision-making power in the higher-level module. That is to say, the way for senior components to treat low-level components is the rule of the bold text above. The book provides an example of coffee and tea (interestingly, coffee and tea is the name of Ding Wei's old album. I bought a genuine album when I was a freshman) to explain the relationship between the te
toolbar
2 Enter the template designer, which is a visual designer that supports drag-and-drop operations, as shown in the figure:
Design the template in the order indicated by the number ordinal of the figure above:
① first change the template name, select the
Template method is better understood, is to avoid code redundancy, implementation of code to take the function. For example, the most common tea and coffee, two have a lot in common, all need to boil water, all need to pour into the cup, then these repeated methods can be drawn out, placed in the parent class, as for the different methods, write an abstract class, so that different subclasses to rewrite the
demand)
Since it is filled, make sure two points. One is that the filled object cannot confuse the disorder, and the second is that the order of the padding cannot be faulted.
This template is injected as a third-party jar dependency, so you must circumvent any business factors.
Think clearly about the focus of the design, and then look at the design
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.