Template refers to the sheet of plastic plate after the knockout, and then use a brush or a color pen to fill the hollowed out part, you can see the pure hand and without losing neat words, see the shape of the hollowing out of the template, immediately know what the final word will be changed, However, the actual appearance of the typeface depends on the type of brush used. Take the black pen as the brush, the result of course is the word of the pen; When you draw with a pencil, it will only be a gray-black pencil, and if you use colorful color pens, you can naturally create a dazzling multi-color word. However, no matter what kind of stationery , the words produced are still not able to remove the shape of the template has been fixed.
Below we illustrate the template method with examples
Relationship between program sample classes
1. template class, equivalent to the sheet plastic sheet we mentioned above
Java code
- Package Com.pattern.templateMethod;
- /**
- * Abstract class, acting as template role
- * @author Administrator
- *
- */
- Public Abstract class Abstractdisplay {
- //abstract method implemented by subclasses
- public abstract Void open ();
- public abstract void print ();
- public abstract Void Close ();
- //abstract class implementation method, final can guarantee that the subclass will not be modified
- public final void display () {
- Open (); //First Open ...
- For (int i=0; i < 5; i++) { //Repeat Output 5 times
- Print ();
- }
- Close (); //output complete, close
- }
- }
2. Character class, output single character
Java code
- Package Com.pattern.templateMethod;
- Public class Chardisplay extends Abstractdisplay {
- private Char ch; //characters to be output
- Public chardisplay (char ch) { //) pass the constructor over the character ch, stored in the field
- this.ch = ch;
- }
- public Void Open () {
- System.out.print ("<<"); //Output "<<" as Start string
- }
- public Void Close () {
- System.out.println (">>"); //Output ">>" as end string
- }
- public void print () {
- System.out.print (CH); //Output the characters stored in the field
- }
- }
3. String class, Output string
Java code
- Package Com.pattern.templateMethod;
- /**
- *
- * @author Administrator
- */
- Public class Stringdisplay extends Abstractdisplay {
- private string string; //String to be output
- private int width; //The "Length" of the string to be evaluated in bytes
- Public Stringdisplay (string string) {
- this.string =string;
- width = string.getbytes (). length;
- }
- public Void Open () { //print head decoration string
- PrintLine ();
- }
- public void print () { //print content
- System.out.println ("|" +string+"|");
- }
- public Void Close () { //print tail decoration string
- PrintLine ();
- }
- public void PrintLine () {
- System.out.print ("+"); //output "+" sign indicates border position
- For (int i=0; i < width; ++i) {
- System.out.print ("-"); //As Segment
- }
- System.out.println ("+"); //output "+" sign indicates border position
- }
- }
4. Test class
Java code
- Package Com.pattern.templateMethod;
- Public class Main {
- public static void Main (string[] args) {
- //Build 1 objects with ' a ' chardisplay
- Abstractdisplay D1 = new Chardisplay (' A ');
- //Build 1 objects with "Hello World" Stringdisplay
- Abstractdisplay D2 = new Stringdisplay ("Hello World");
- //d1,d2 are Abstractdisplay subclass objects that can call the inherited display () method
- D1.display ();
- D2.display ();
- }
- }
Output:
C code
- <<AAAAA>>
- +-----------+
- | Hello world|
- | Hello world|
- | Hello world|
- | Hello world|
- | Hello world|
- +-----------+
Design ideas:
As a template method is defined in the parent class (the parent class is an abstract class), and the method definition uses the abstract method, the implementation of the abstract method is a subclass, in order to implement the method in the subclass to determine the specific operation. If you implement different implementations in different subclasses, you can develop different processing content. However, regardless of the subclass in which the implementation is performed, the approximate process of processing is still based on the way the parent class is developed.
Java design pattern-template method