Template method Mode (Templatemethod pattern): Examples of template methods and hooks used in Java

Source: Internet
Author: User
Tags comparable
use of template methods in the Java API


1.arrays.sort () sort


When sorting arrays, we often use the Arrays.sort () method, and the sort () method of the Arrays class is the template method.


The focus of the template method pattern is to provide an algorithm that and let subclasses implement certain steps, but not when the Arrays.sort () array is sorted, usually we can't design a class to inherit a Java array, and the sort () method wants to be able to apply to all arrays (each array is a different Class). So a static sort () method is defined that requires an algorithm portion of the element's size in the template method sort (), but there are different collations for different classes. So let each element in the sorted object provide the algorithm part of the comparison size (the way is the class of the sorted element must implement the comparable interface).


Arrays.sort () array sorting is not a standard implementation of the template method as previously mentioned, but its implementation still conforms to the spirit of the template method pattern, and furthermore, the algorithm can be implemented without inheriting an array, which makes the ordering more flexible and useful.


The common class Integer, string, and so on in Java have already implemented the comparable interface, so you can sort the array of that type directly using Arrays.sort (), and if you need to sort the array of custom types, Then our custom class must implement the comparable interface.


the Read () method of the 2.java.io.inputstream class


The Java.io.InputStream abstract class is a superclass of all classes that represent the byte input stream. The read (byte b[],int off,int len) is a template method in which the abstract method read () of the class is invoked, requiring subclasses to provide implementations of the read () method. This is a typical use of template method patterns.


hooks in the 3.Swing window program: Paint () method


By default the paint () method is not doing things (that is, NULL implementation) (JFrame component in the superclass of the paint () is null implemented), it is a "hook", by overwriting the paint () method, we can insert our own code into the JFrame algorithm, Show the desired picture.

Cases:

public class MyFrame extends JFrame
{public
	myframe (String titile)
	{
		super (titile);
		This.setdefaultcloseoperation (exit_on_close);
		This.setsize (in);
		This.setvisible (true);

	@Override public
	void Paint (Graphics g)
	{
		super.paint (g);
		g.DrawString ("Paint" () is a hook ",");
	}

	public static void Main (string[] args)
	{
		MyFrame f = new MyFrame ("Swing");
	}

Effect:


hooks in the 4.Applet


The methods such as Init (), Start (), Stop (), Destroy (), and so on in the applet class are all NULL implementations, and subclasses can selectively override these methods for extension.


5. Other

。。。。。。。


use of template method patterns in Java technology or open source framework


Use of template method patterns in JUnit


Other...



Reprint please indicate the source: http://blog.csdn.net/jialinqiang/article/details/8869044

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.