Zen-Template Method pattern of design pattern

Source: Internet
Author: User

Personal blog This blog post location: http://www.sanyinchenblog.com/?p=273

Template method Mode:

Defines an algorithm framework in an operation that delays some steps into subclasses. Enables subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.

Demo:

Https://github.com/sanyinchen/UMLDemo/tree/master/src/com/sanyinchen/templete

Https://github.com/sanyinchen/UMLDemo/tree/master/src/com/sanyinchen/templete2

Introduce the background of the book, the background is to do a car model.

The class diagram is this:




Client class:


Package Com.sanyinchen.templete;public class Client {public static void main (string[] args) {//TODO auto-generated method Stubhummermodel Hummermodel = new Hummerh1model1 (); Hummermodel.start (); Hummermodel.engineboom (); HummerModel.alarm ( ); Hummermodel.run (); System.out.println ("--------------------"); Hummermodel = new Hummerh1model2 (); Hummermodel.start (); Hummermodel.engineboom (); Hummermodel.alarm (); Hummermodel.run ();}}

Hummermodel:


Public abstract class Hummermodel {public abstract void start ();p ublic abstract void Stop ();p ublic abstract void alarm (); /horn tweet public abstract void engineboom ();//engine Roar public abstract void Run ();}

hummerh1model1

package Com.sanyinchen.templete;public class Hummerh1model1 extends HummerModel { @Overridepublic void Start () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model1 initiated");} @Overridepublic void Stop () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model1 stopped");} @Overridepublic void Alarm () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model1 horn called");} @Overridepublic void Engineboom () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model1 engine Started");} @Overridepublic void Run () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model1 ran Up");}} 

Hummerh1model2


public class Hummerh1model2 extends Hummermodel {@Overridepublic void start () {// TODO auto-generated Method StubSystem.out.println ("Hummerh1model2 started Up");} @Overridepublic void Stop () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model2 stopped");} @Overridepublic void Alarm () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model2 horn called");} @Overridepublic void Engineboom () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model2 engine Started");} @Overridepublic void Run () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model2 ran Up");}} 
the structure of the program run:



Hummerh1model1 started the Hummerh1model1 engine, started the Hummerh1model1 horn, called Hummerh1model1, ran up--------------------Hummerh1model2 started Hummerh1mode. The L2 engine started the Hummerh1model2 horn called Hummerh1model2 and ran up.
of course, we can encapsulate each step in the Run method:


HUMMERH1MODEL3:


package Com.sanyinchen.templete;public class Hummerh1model3 extends HummerModel { @Overridepublic void Start () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model3 initiated");} @Overridepublic void Stop () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model3 stopped");} @Overridepublic void Alarm () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model3 horn called");} @Overridepublic void Engineboom () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model3 engine Started");} @Overridepublic void Run () {//TODO auto-generated method Stubthis.start (); This.engineboom (); This.alarm (); System.out.println ("Hummerh1model3 ran Up");}} 
This is a basic template design pattern, but we can clearly see that there will be duplicate code, if you follow model3 that way, then model4? The method inside the run () is the same.


So we can turn the Hummermodel method into an implementation method, so it doesn't have to be implemented again when the other model inherits it.

Advantages of the template method:

(1) Package invariant part, extend variable part

(2) Extracting public part code for easy maintenance

(3) The behavior is controlled by the parent class, and the subclass implements

Template method Pattern Usage scenarios:

(1) Multiple subclasses have public methods, and the logic is basically the same

(2) important, complex code, the core algorithm can be designed as a template method, the surrounding details of the function is implemented by each sub-class

(3) When refactoring, the template method pattern is a frequently used pattern, drawing the same code into the parent class, and then constraining its behavior through the hook function.

Extension of the template method:

The following need to modify the requirements, Model1 and model2 horn does not sound is controlled, so add a hook function.

The class diagram after the modification:



Client

Package com.sanyinchen.templete2;/** * Template method mode hook function using *  * @author Sanyinchen * */public class Client {public static voi D main (string[] args) {//TODO auto-generated method Stubhummermodel Hummermodel = new Hummerh1model1 (); Hummermodel.run () ; System.out.println ("--------------------"); Hummermodel = new Hummerh1model2 (); Hummermodel.run (); System.out.println ("--------------------"); Hummerh1model3 hummerMode3 = new Hummerh1model3 (); Hummermode3.stearam (false); Hummermode3.run ();}}

Hummermodel:


Package Com.sanyinchen.templete2;public Abstract class Hummermodel {public abstract void start ();p ublic abstract void sto P ();p ublic abstract void alarm ();//horn tweet public abstract void engineboom ();//Engine Roar public Abstract Boolean isalarm ();p UB LIC void Run () {//TODO auto-generated method Stubthis.start (); This.engineboom (); This.alarm ();}}

Hummerh1model3

Package Com.sanyinchen.templete2;public class Hummerh1model3 extends Hummermodel {private Boolean isarm = true;@ overridepublic void Start () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model3 initiated"); @Overridepublic void Stop () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model3 stopped");} @Overridepublic void Alarm () {//TODO auto-generated method Stubif (This.isalarm ()) System.out.println (" Hummerh1model3 Horn was called ");} @Overridepublic void Engineboom () {//TODO auto-generated method StubSystem.out.println ("Hummerh1model3 engine Started");} @Overridepublic Boolean isalarm () {//TODO auto-generated method Stubreturn this.isarm;} public void Stearam (Boolean flag) {this.isarm = flag;}}


Operation Result:


Hummerh1model1 started the Hummerh1model1 engine, started the Hummerh1model1 horn called--------------------Hummerh1model2 started the Hummerh1model2 engine started Hummerh1mod El2 Horn called--------------------Hummerh1model3 started the HUMMERH1MODEL3 engine.


It can be seen that the model3 to horn is determined by the scene class.



Zen-Template Method pattern of design pattern

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.