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