Template methods Mode (Templete method)
Defining the skeleton of an algorithm in an operation, and delaying the implementation of some mutable parts into subclasses, the template method pattern allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
Cases:
1 PackagePractice1;2 3 ImportJava.util.Random;4 5 Public classTest8 {6 Public Static voidMain (String []args) {7Games g=NewGirl ();8 G.play ();9 }Ten } One Abstract classgames{ A Public voidPlay () {//defining an algorithmic skeleton in an operation -SYSTEM.OUT.PRINTLN ("Preparation"); -System.out.println ("Start"); the if(Result ()) { -System.out.println ("won"); -}Else{ -System.out.println ("Lost"); + } - } + A Public Abstract BooleanResult ();//delay implementation of some mutable parts into subclasses at } - - classGirlextendsgames{ - - @Override - Public Booleanresult () { inRandom r=NewRandom (); - returnR.nextboolean (); to } + - } the classModegirlextendsgames{ * $ @OverridePanax Notoginseng Public Booleanresult () { - return false; the } + A}
Java abstract class Application-template method pattern