In general, design patterns fall into three broad categories:
Create Patterns 5: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode.
Structure Mode 7 types: Adapter mode, adorner mode, proxy mode, appearance mode, bridge mode, combined mode, and enjoy meta mode.
There are 11 types of behavioral Patterns: Policy mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, Command mode, Memo mode, state mode, visitor mode, mediator mode, interpreter mode.
Template Method ModeThe key to implementing the template pattern through the relationship between the parent class and the subclass is that the subclass can displace the mutable part of the parent class, but the subclass cannot change the top-level logic represented by the template method.
the methods in the template method can be divided into two main categories: Template method and Basic method.
Template Method① A template method is defined in an abstract class that combines basic operational methods to form a total algorithm or a method of total behavior. ② an abstract class can have any number of template methods, not limited to one. Each template method can invoke any number of specific methods.
Basic Methods
The basic method can be divided into three kinds: abstract method, Concrete method (concrete methods) and hook method. ①
Abstract MethodsAn abstract method is declared by an abstract class and implemented by a specific subclass. In the Java language, abstract methods are indicated by the Abstraction keyword. Ii
Specific MethodsAn abstract class can have any number of template methods, not limited to one. Each template method can invoke any number of specific methods. ③
Hook MethodA hook method is declared and implemented by an abstract class, and subclasses are extended. Usually the implementation given by an abstract class is an empty implementation, as the default implementation of the method. The name of the hook method should start with do
Demo: Cover House case
First, write an abstract class
public abstract class housetemplate { public final void Buildhouse () {// template method, final decoration, you can not quilt class rewrite buildfoundation (); // Play Foundation Buildpillars (); // Building Pillars buildwalls (); // Wall coverings buildwindows (); // do Windows system . Out.println (" The house is well covered ""; } private void Buildwindows () {system . Out.println (); } public abstract void Buildwalls (); public abstract void Buildpillars (); private void Buildfoundation () {system . Out.println ("; }}
Second, write the implementation class
class WoodenHouse extends HouseTemplate {//木头房子 @Override public void buildWalls() { System.out.println("做木墙"); } @Override public void buildPillars() { System.out.println("立木柱"); }}
class GlassHouse extends HouseTemplate {//玻璃屋 @Override public void buildWalls() { System.out.println("做玻璃墙"); } @Override public void buildPillars() { System.out.println("立玻璃柱"); }}
then, write the test class
class HousingClient {public static void main(String[] args) {HouseTemplateWoodenHouse();//造木屋houseType.buildHouse();//调用策略方法System.out.println("------------"GlassHouse();//造玻璃屋houseType.buildHouse();}}
Finally, check out the results
Use cement, iron rods, sand to play the foundation
Vertical Pillars
Make a wooden wall
Making Windows
The house is covered.
————
Use cement, iron rods, sand to play the foundation
Vertical Pillars
Make a glass wall
Standing Glass Column
The house is covered.
use of template method patterns for IO streams and util in JDKInputStream, Outputstream,reader, and all non-abstract methods in writer. Abstractlist, Abstractset, and all non-abstract methods in Abstractmap.
Important Notes
the #① template method should consist of determined steps. The order of these steps is fixed. Some methods or implementations can differ between a base class and a subclass. The template method should be final.
② Most of the time, the method of calling a subclass is derived from the superclass. However, in template mode, the method called by the superclass template method comes to the subclass, which is the famous Hollywood principle-"Don t call us, we'll be calling you".
the default implementation of the ③ base class method is degraded to the concept of hook hooks, they are designed to be overridden in subclasses, and if you expect some methods to be overridden in subclasses, you can make them final. For example, the Buildfoundation () method is final because you do not want it to be overridden in a subclass.
Template method pattern of design pattern