Template method Mode:
The template method pattern is defined as an algorithmic framework in an operation, and some steps are deferred to subclasses so that subclasses can redefine some specific steps of the algorithm without altering the structure of an algorithm.
In short, it is to define an abstract class in which there are some methods that require a subclass-specific implementation, and a method that is basically implemented unchanged, and in this fixed method calls those methods that require a subclass implementation, thus achieving some steps to defer to the subclass.
Therefore, there are generally two types of methods in the template method pattern:
1, basic method.
The method is implemented by subclasses and is called in the template method.
2, Template method.
General is a concrete method to implement the basic method of scheduling, complete the fixed logic.
For example, a class at a party, everyone has to perform singing and dancing, both programs.
Embodied in the code is as follows:
1 //Abstract template class2 Public Abstract classStudentshow {3 4 //Basic Method 15 Public Abstract voidsing ();6 //Basic Method 27 Public Abstract voiddance ();8 //template method, for basic method invocation9 Public voidShow () {Ten This. Sing (); One This. Dance (); A } - } - //implementing subclasses specifically the Public classJackshow extends Studentshow { - - Public voidSing () { -System. out. println ("Jack sings!!!"); + } - Public voidDance () { +System. out. println ("Jack, Dance!!! ."); A } at } - Public classRoseshow extends Studentshow { - - Public voidSing () { -System. out. println ("Rose sings!!!"); - } in Public voidDance () { -System. out. println ("Rose dancing!!!"); to } +}
Specific scenario classes:
1 Public classClient {2 Public Static voidMain (string[] args) {3Studentshow Jack =Newjackshow ();4Studentshow rose =Newroseshow ();5 6 jack.show ();7 rose.show ();8 }9}
This is the template method mode, or relatively simple.
But what if Jack only sings and doesn't dance, doesn't dance and sings?
1 //abstract template class with control template method2 Public Abstract classStudentshow {3 4 //Basic Method 15 Public Abstract voidsing ();6 //Basic Method 27 Public Abstract voiddance ();8 9 //Conditional selection JudgmentTen Public Booleanisdance () { One return true; A } - - //template method, for basic method invocation the Public voidShow () { - This. Sing (); - if(Isdance ()) { - This. Dance (); + } - } + } A //concrete Implementation Class 1 at Public classJackshowextendsStudentshow { - - Public voidDance () { -SYSTEM.OUT.PRINTLN ("Jack!!!"); - } - Public voidSing () { inSystem.out.println ("Jack Dances!!!"); - } to //because Jack does not dance, the method of controlling the condition in the template class is rewritten. Modify the return value to False + Public Booleanisdance () { - return false; the } * } $ //concrete Implementation Class 2Panax Notoginseng //a subclass that has no modifications to the business does not need to be changed, greatly increasing extensibility. - Public classRoseshowextendsStudentshow { the + Public voidDance () { ASystem.out.println ("Rose dancing!!!"); the } + Public voidSing () { -System.out.println ("Rose sings!!!"); $ } $ } - //There is no need to change the specific scene class - Public classClient { the Public Static voidMain (string[] args) { -Studentshow Jack =Newjackshow ();WuyiStudentshow rose =Newroseshow (); the - jack.show (); Wu rose.show (); - } About}
Therefore, we can use some conditions to control the template method, call the order of the basic methods, so as to achieve different business requirements.
23 Design Mode----------Template method mode