Template method Mode:
Defined:
Defines an algorithmic framework in an operation, and delays some steps into subclasses. Enables subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
The template method pattern is very simple and only uses the Java inheritance mechanism, but it is a very wide application pattern.
In a software development project, if the same piece of code was duplicated two times, the design would need to be doubted, and the architect would clearly explain why the same logic would occur two or more times.
Advantages of the template method pattern:
Package invariant, extended variable part
Extract public part code for easy maintenance
The behavior has the parent class control, the subclass implements.
Disadvantages:
Subclasses have an effect on the parent class.
Production of Hummer vehicle models:
Class Diagram:
The Hummer car has two models: H1 and H2.
Abstract Hummer Interface:
Abstract class Hummermodel {//Start public abstract void Start ();//Stop public abstract void Stop ();//trumpet public abstract void Alarm ();//engine ring public abstract void engineboom ();//run up public abstract void run ();
Two types of Hummer:
H1 Hummer Model Class Hummerh1model extends Hummermodel {public void start () {System.out.println ("started up");} public void Stop () {System.out.println ("stopped");} public void Alarm () {System.out.println ("horn rang"); public void Engineboom () {System.out.println ("engine launch");} public void Run () {//Start car This.start ();//engine this.engineboom ();//Horn Ring this.alarm ();//parking this.stop ();}} H2 type class Hummerh2model extends Hummermodel {public void start () {System.out.println ("started up");} public void Stop () {System.out.println ("stopped");} public void Alarm () {System.out.println ("horn rang"); public void Engineboom () {System.out.println ("engine launch");} public void Run () {//Start car This.start ();//engine this.engineboom ();//Horn Ring this.alarm ();//parking this.stop ();}}
The code in the Run method in the two implementation classes is the same, and the implementation of the Run method should be in the abstract class, not in the implementation class.
This is done after the change:
Abstract class Hummermodel {//Start public abstract void Start ();//Stop public abstract void Stop ();//trumpet public abstract void Alarm ();//engine ring public abstract void engineboom ();//run up public void run () {///start Auto This.start ();//Engine Launch This.engineboom () ;//Horn Ring this.alarm ();//parking this.stop ();}} H1 Hummer Model Class Hummerh1model extends Hummermodel {public void start () {System.out.println ("Hummer H1----> Started up");} public void Stop () {System.out.println ("Hummer H1----> stopped"); public void Alarm () {System.out.println ("Hummer H1----> Horn rang");} public void Engineboom () {System.out.println ("Hummer H1----> Engine launch");}} H2 type class Hummerh2model extends Hummermodel {public void start () {System.out.println ("Hummer H2----> Started up");} public void Stop () {System.out.println ("Hummer H2----> stopped"); public void Alarm () {System.out.println ("Hummer H2----> Horn rang");} public void Engineboom () {System.out.println ("Hummer H2----> Engine launch");}}
The Run method in the abstract class Hummermodel class is called the template method, and the other method is called the basic method. Template method to implement the basic method of scheduling, the completion of fixed business logic.
Scene class:
public class Client {public static void main (string[] args) {Hummermodel H1 = new Hummerh1model (); H1.run (); Hummermodel h2 = new Hummerh2model (); H2.run ();}}
The basic method in the abstract template class is designed as protected type, conforming to the Dimitri Law, and implementing the class is not necessary to extend the access rights in the parent class if possible.
Extension of template Method pattern:
The Hummer Horn of the H1 model wants it to ring, the H2 model Horn does not have the sound.
Add an implementation method to the abstract class Isalarm, with each class covering the method.
Abstract class Hummermodel {//start protected abstract void Start ();//stop protected abstract void stop ();//horn ring protected ABSTR ACT void Alarm ();//engine ring protected abstract void engineboom ();//run up public void run () {//Start auto This.start ();//Engine launch This.eng Ineboom ();//Horn ring if (This.isalarm ()) {this.alarm ();} Parking this.stop ();} The hook method, the default speaker is a ringing protected Boolean isalarm () {return true;}} H1 Hummer Model Class Hummerh1model extends Hummermodel {private Boolean alarmflag = true;protected void Start () {System.out.prin TLN ("Hummer H1----> Started up");} protected void Stop () {System.out.println ("Hummer H1----> stopped"); protected void Alarm () {System.out.println ("Hummer H1----> Horn rang");} protected void Engineboom () {System.out.println ("Hummer H1----> Engine launch"); Protected Boolean isalarm () {return this.alarmflag;} public void Setalarm (Boolean alarmflag) {this.alarmflag = Alarmflag;}} H2 type class Hummerh2model extends Hummermodel {protected void start () {System.out.println ("Hummer H2----> Started up");} protected void Stop () {System.out.println ("Hummer h2----> stopped "); protected void Alarm () {System.out.println ("Hummer H2----> Horn rang");} protected void Engineboom () {System.out.println ("Hummer H2----> Engine launch"); Protected Boolean isalarm () {return false;}}
Scene class:
public class Client {public static void main (string[] args) throws IOException {Hummermodel H1 = new Hummerh1model (); System.out.println ("-------------H1 model Hummer---------------"); System.out.println ("Does the H1 model Hummer need a horn ring?") 0-does not require 1-need "); String type = (new BufferedReader (New InputStreamReader (system.in))). ReadLine (); if (Type.equals ("0")) {( Hummerh1model) H1). Setalarm (false);} H1.run (); System.out.println ("-------H2 models of hummer----"); Hummermodel h2 = new Hummerh2model (); H2.run ();}}
Users can control the H1 model of the car horn, external conditions change, affecting the implementation of the template method.
The Isalarm method in an abstract class is called a hook method.
A template method with a hook method is a perfect model.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Design mode (4): Template method mode