Java design mode Template Method mode template Method, design mode Template
Design pattern is a set of summary of code Design experiences that are repeatedly used, known to most people,
Java Design Pattern Template Method (template Method), design pattern Template
This article continues to introduce the template method modes of the 23 design patterns series.
Overview T
Java design pattern: template method pattern or template design pattern (behavior pattern), java Design Pattern
Abstract class, the unchanged code is written, and the part to be changed is left to the subclass for implementation:
Package behavior _
Template methods (Template method) mode
Prepare an abstract class, implement part of the logic in the form of concrete methods and concrete constructs, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the purpose of th
Abstract class, unchanging code is written, to change the part left to the subclass to achieve:Package Behavior _ template method mode;//template design mode means that the invariant positioning part is written out, the change of the part left to the subclass to achieve public abstract class GetTime {public long GetTime () throws Exception{long start = System.cur
Java Design Pattern template method pattern, java Design Pattern TemplateTemplate Method Mode
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.In l
Article Description: personally feel that Lofter is a useful tool first, then it is possible to become a good community. Lofter must be simple to use, have a concise release operation Port (computer page and mobile client), and exquisite display space (blog template and Timeline page); Yes, exquisite display space--blog templates.
In charge of lofter template
. (Template method pattern: definethe skeleton of an algorithm in an operation, deferring some steps tosubclasses. template methodletssubclasses redefine certain steps of an algorithm the algorithm's structure .)
1) The template method mode is a basic technique for reusing inherited code. The structure and usage of the templ
Define: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. template method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.1. template method mode
Prepare an abstract class, implement part of the logic in the form of a specific method and a specific constructor, and then declare some abstract methods to force the subclas
that the instance must be created automatically and provided externally. AdvantagesThere is only one object in system memory, so system resources can be saved, and for some object singleton patterns that need to be created and destroyed frequently, the performance of the system can be improved. DisadvantagesThere is no abstraction layer, so the extension is difficult.Too heavy a duty to violate a single duty in a certain procedureTemplate design mo
1. Template method Mode
Prepare an abstract class, implement part of the logic in the form of concrete methods and concrete constructs, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the purpose of the template metho
Prepare an abstract class, implement part of the logic in the form of concrete methods and concrete constructs, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the purpose of the template method pattern.
Many people may not think that the
Because of its own defects, the inheritance relationship has been detained by experts as "evil. Experts such as "using delegate relationships instead of inheritance relationships" and "Using Interfaces as much as possible instead of abstract class inheritance" warn us that these cainiao will "look at inheritance differently ". In fact, inheritance still has many advantages. It seems that the disadvantages are even more obvious. The rational use of the inheritance relationship can still play a go
Defines the algorithm skeleton in an operation, and delays some steps to the subclass, so that the subclass can not change the structure of an algorithm, but define some specific steps of the algorithm inheritance relationship due to its own defects, the experts have taken down the "evil" hat. Experts such as "using delegate relationships instead of inheritance relationships" and "using interfaces as much as possible instead of abstract class inheritance" warn us that these Cainiao will "look at
Due to its own shortcomings, the inheritance relationship has been buttoned up by experts on the "evil" hat. "Use delegated relationships instead of inheritance relationships", "Try to use interface implementations instead of abstract class inheritance" and so on experts warn, let us these rookie to inherit "Respect". In fact, inheritance or have a lot of their own advantages. It seems that the shortcomings are more obvious than those abused by everyone. Reasonable use of the inheritance relatio
Defines the algorithm skeleton in an operation, and delays some steps to the subclass, so that the subclass can not change the structure of an algorithm, but define some specific steps of the algorithm inheritance relationship due to its own defects, the experts have taken down the "evil" hat. Experts such as "using delegate relationships instead of inheritance relationships" and "using interfaces as much as possible instead of abstract class inheritance" warn us that these Cainiao will "look at
methods that subclass may override;
3. UML
In the UML diagram, the templatemethod in the parent class is an abstract method, which calls four methods in a certain order-primitiveopertion1, primitiveopertion2, concreteopertion, and hook. primitiveopertion1 and operator are abstract methods, child classes are required to be implemented based on specific conditions, but the overall framework of the algorithm is controlled by the templatemethod method in the parent class.
4. UseFor the c
"Design pattern Refinement" Learning notes (14) ------Template method (Template method) mode
GOF : defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Template method enables subclasses to redefine certain steps of the algorithm without altering the structure of an algorithm
The structural mode is over, and the behavioral mode is started.
The first is the simplest, easiest, and most commonly used template method mode.How do we define the template method mode? Simply put, it is to first define a skeleton (Framework Developer), and then implement the specific application (ApplicationProgramDeveloper) the specific content of such a skeleton, which is the
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.