When we are going to complete a process or a series of steps that are consistent at a certain level of detail, but an individual step is not implemented at a detailed level, we usually consider using a template method pattern to handle it.
Template method Pattern: Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
The template method pattern is to realize its advantage by moving the invariant behavior to the superclass and removing the duplicated code in the subclass.
Package Templatemethod; Public Abstract class Template { publicabstractvoid operation1 (); Public Abstract void Operation2 (); Public void Templatemethod () { operation1 (); Operation2 (); }}
PackageTemplatemethod; Public classConcreateclassaextendsTemplate {@Override Public voidOperation1 () {System.out.println ( This+ "Opertion1"); } @Override Public voidOperation2 () {System.out.println ( This+ "Opertion2"); } Public Static voidMain (string[] args) {Template template; Template=NewConcreateclassa (); Template.templatemethod (); }}
Big talk design mode Note Template method mode (common)