Java design pattern-template method

Source: Internet
Author: User

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
  1. Package Com.pattern.templateMethod;
  2. /**
  3. * Abstract class, acting as template role
  4. * @author Administrator
  5. *
  6. */
  7. Public Abstract class Abstractdisplay {
  8. //abstract method implemented by subclasses
  9. public abstract Void open ();
  10. public abstract void print ();
  11. public abstract Void Close ();
  12. //abstract class implementation method, final can guarantee that the subclass will not be modified
  13. public final void display () {
  14. Open (); //First Open ...
  15. For (int i=0; i < 5; i++) { //Repeat Output 5 times
  16. Print ();
  17. }
  18. Close (); //output complete, close
  19. }
  20. }

2. Character class, output single character

Java code
  1. Package Com.pattern.templateMethod;
  2. Public class Chardisplay extends Abstractdisplay {
  3. private Char ch; //characters to be output
  4. Public chardisplay (char ch) { //) pass the constructor over the character ch, stored in the field
  5. this.ch = ch;
  6. }
  7. public Void Open () {
  8. System.out.print ("<<"); //Output "<<" as Start string
  9. }
  10. public Void Close () {
  11. System.out.println (">>"); //Output ">>" as end string
  12. }
  13. public void print () {
  14. System.out.print (CH); //Output the characters stored in the field
  15. }
  16. }

3. String class, Output string

Java code
  1. Package Com.pattern.templateMethod;
  2. /**
  3. *
  4. * @author Administrator
  5. */
  6. Public class Stringdisplay extends Abstractdisplay {
  7. private string string; //String to be output
  8. private int width; //The "Length" of the string to be evaluated in bytes
  9. Public Stringdisplay (string string) {
  10. this.string =string;
  11. width = string.getbytes (). length;
  12. }
  13. public Void Open () { //print head decoration string
  14. PrintLine ();
  15. }
  16. public void print () { //print content
  17. System.out.println ("|"  +string+"|");
  18. }
  19. public Void Close () { //print tail decoration string
  20. PrintLine ();
  21. }
  22. public void PrintLine () {
  23. System.out.print ("+"); //output "+" sign indicates border position
  24. For (int i=0; i < width; ++i) {
  25. System.out.print ("-"); //As Segment
  26. }
  27. System.out.println ("+"); //output "+" sign indicates border position
  28. }
  29. }

4. Test class

Java code
  1. Package Com.pattern.templateMethod;
  2. Public class Main {
  3. public static void Main (string[] args) {
  4. //Build 1 objects with ' a ' chardisplay
  5. Abstractdisplay D1 = new Chardisplay (' A ');
  6. //Build 1 objects with "Hello World" Stringdisplay
  7. Abstractdisplay D2 = new Stringdisplay ("Hello World");
  8. //d1,d2 are Abstractdisplay subclass objects that can call the inherited display () method
  9. D1.display ();
  10. D2.display ();
  11. }
  12. }

Output:

C code
    1. <<AAAAA>>
    2. +-----------+
    3. | Hello world|
    4. | Hello world|
    5. | Hello world|
    6. | Hello world|
    7. | Hello world|
    8. +-----------+

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.