Design pattern--3. Template Method Mode

Source: Internet
Author: User

   

1. Template Method mode
Defines the framework for an algorithm in an operation, and delays some steps into subclasses. Enables a subclass to redefine some specific steps of the algorithm without altering the structure of an algorithm.
The template method pattern uses the inheritance mechanism in Java, and the extraction class is called the drawing template, and its methods are divided into two categories
(1) Basic method: A method implemented by a subclass and called in a template method
(2) Template method: You can have one or several (that is, a framework) to implement the basic method of the call, complete the fixed logic.
To prevent malicious operations, the generic template method is prefixed with the final keyword and is not allowed to be overridden.

2. In the abstract class, there is a Isalarm method, the template method according to its return value determines whether to ring horn, the method affects the template method execution results, the method called Hook method

3. Advantages
(1) Package invariant part, extend variable part
(2) Extraction of public code, easy to maintain
(3) The behavior is controlled by the parent class, and the subclass implements

4. Disadvantages
(1) The execution result of the subclass affects the parent class, which affects the readability of the code.

5. Usage Scenarios
(1) Multiple subclasses have public methods, and the logic is basically the same
(2) important, complex algorithms, can be the core part of the design as a template method, the surrounding related functions and details of sub-class implementation.

Abstractcar class

 Public Abstract classAbstractcar {protected Abstract voidstart (); protected Abstract voidStop (); protected Abstract voidAlarm (); protected BooleanIsalarm () {return false; }        Final  Public voidrun () { This. Start (); if( This. Isalarm ()) {             This. Alarm (); }                 This. Stop (); }}

Audi Class

 Public classAodicarextendsAbstractcar {@Overrideprotected voidstart () {System.out.println ("Aodi start ..."); } @Overrideprotected voidStop () {System.out.println ("Aodi stop ..."); } @Overrideprotected voidAlarm () {System.out.println ("Aodi Alarm ..."); } @Overrideprotected BooleanIsalarm () {return false; }     }

BMW Class

/** Isalarm is an implementation method. The function is that the template method determines whether to ring the horn based on its return value, and the subclass can override the return value*/ Public classBaomacarextendsAbstractcar {Private BooleanAlarmflag =false; @Overrideprotected voidstart () {System.out.println ("Baoma Car start ..."); } @Overrideprotected voidStop () {System.out.println ("Baoma Car Stop ..."); } @Overrideprotected voidAlarm () {System.out.println ("Baoma Car Alarm ..."); }        protected BooleanIsalarm () {return  This. Alarmflag; }          Public voidSetalarmflag (Booleanflagtmp) {         This. Alarmflag =flagtmp; }}

Scene class, Main

 Public class Main {        publicstaticvoid  main (string[] args) {        new Baomacar ();        Bm.setalarmflag (true);        Bm.run ();                 New Aodicar ();        Ac.run ();    }}

Design pattern--3. Template Method Mode

Related Article

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.