Java design Mode _ (behavior) _ Template Method mode __java

Source: Internet
Author: User
Tags abstract

Quote Encyclopedia

The template method pattern is one of the most common patterns in all patterns, and is based on the basic technology of inherited code reuse.
Template method patterns require the development of collaboration between abstract classes and designers of specific subclasses. It is the behavior pattern of the class, prepares an abstract class, implements part of the logic in the form of concrete methods and concrete constructors, and then declares some abstract methods to force the subclass to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the intent of the template method pattern, where the behavior represented by the template method is called the top-level behavior, and its logic is called the top-level logic.


Instructions for use

Define the skeleton of an algorithm in one method, and delay some steps into subclasses. The template method enables subclasses to redefine certain steps in the algorithm without changing the algorithm structure. The main method is defined as final in the template, which prevents subclasses from modifying the algorithm skeleton, and defines the method that the subclass must implement as abstract. The common method (no final or abstract modification) is called a hook.


Related Roles

1. Abstract Template role:
One or more abstract operations are defined in order for subclasses to be implemented. These abstract operations are called basic operations, and they are the constituent steps of a top-level logic.
Defines and implements a template method. This template method is generally a concrete method that gives the skeleton of a top-level logic, and the logical composing step is deferred to the subclass implementation in the corresponding abstract operation. It is also possible for top-level logic to invoke some specific methods.

2, the specific template role (templates subclass):
Implements one or more abstract methods defined by the parent class, which are the constituent steps of a top-level logic.
Each abstract template role can have any number of specific template roles to correspond to, and each specific template role can give different implementations of these abstract methods (i.e., the constituent steps of the top-level logic), thus making the implementation of the top-level logic varied.


Concrete Implementation



Related Code

1. Abstract template Role Class

Abstract template Role Class public
abstract class Abstracttemplate {

	//Template method public
	final void request (String msg) {
		MethodA (msg);
		MethodB (msg);
		Success ();
	}

	The basic method leaves the subclass to implement public
	abstract void MethodA (String msg);

	The basic method leaves the subclass to implement public
	abstract void MethodB (String msg);

	The basic method has been implemented
	with the private void success () {
		System.out.println ("Perform processing complete!");
		Business processing Logic ...}}


2, specific template implementation class

Template specific implementation Class A public
class Realemplatea extends Abstracttemplate {

	@Override public
	void MethodA (String msg) {
		System.out.println ("Class A Business a processing ..." +msg);
	}

	@Override public
	void MethodB (String msg) {
		System.out.println ("Class A business B processing ..." +msg);
	}

Template specific implementation Class B public
class Realemplateb extends Abstracttemplate {

	@Override public
	void MethodA (String msg) {
		System.out.println ("Class B Business a processing ..." + msg);
	}

	@Override public
	void MethodB (String msg) {
		System.out.println ("Class B Business B processing ..." + msg);
	}

3. Client-side test

public class Client {public

	static void Main (string[] args) {
		//create template
		abstracttemplate tmp = new Realempla Tea ();
		Tmp.request ("test");

		TMP = new Realemplateb ();
		Tmp.request ("Publish");
	}


The above code is simple to implement the template method mode, run the output:

Class A business a processing ... Test
Class A Business B processing ... Test
Perform processing complete!
Class B Business a processing ... Release
B-class business B processing ... Release
Perform processing complete!

The key to template mode is that subclasses can displace the variable parts of the parent class, but subclasses cannot change the top-level logic represented by the template method.
Whenever you define a new subclass, you should consider which operations must be replaced, which operations are replaceable, and which operations cannot be replaced. Using template patterns can make these responsibilities clear.


pros and Cons:

Advantages:
1 The template method pattern defines the algorithm formally in a class, and its subclasses implement the processing of the details.
2 Template method is a basic technique for code reuse. They are particularly important in class libraries, which extract public behavior from the class library.
3 The template method pattern leads to a reverse control structure, which adds new behavior through the extension of the subclass and conforms to the "open and close" principle.

Disadvantages:
1 each implementation needs to define a subclass, which causes the number of classes to increase, the system larger and the design more abstract.


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.