"Design pattern Refinement" Learning notes (14)------Template Method (template methods) mode

Source: Internet
Author: User
Tags inheritance

"Design pattern Refinement" Learning notes (14) ------Template method (Template method) mode

GOF : defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Template method enables subclasses to redefine certain steps of the algorithm without altering the structure of an algorithm.

Inheritance is one of the three characteristics of Oo thought (encapsulation, inheritance, polymorphism). Inheritance is for better code reuse, but today, more and more OO methodology gurus are summing up more than one place: prioritize combinations rather than inheritance. The Template method pattern is one of the easiest models to learn at the moment. This is also a more commonly used pattern. Inheritance is fully applied in Template method mode. In fact, in my opinion, the Template method pattern is inherited. In a drawing program, I wrote a method called Drow, which will draw the graphics I need. The Template method pattern can help me do this because the invocation of the methods is done through a reference that refers to an object that belongs to a derived class.

Here's template method. schema UML Figure:

The following is the implementation of the Java Code:

Defines an abstract class that defines the skeleton of an algorithm, here is the abstract drow.

Package Templatemethod;

Public abstract class Shapetemplate

{

public abstract void Drow (),//template method

}//end Abctract Class shape

Package Templatemethod;

derives a concrete class and implements the method drow in the concrete class.

public class Square extends Shapetemplate

{

@Override

public void Drow ()

{

System.out.println ("Draw a square");

}//end Drow ()

}//end class Square

Package Templatemethod;

Test code:

public class Templatemethodpattern

{

Private Shapetemplate Square;

Public Templatemethodpattern ()

{

Square = new Square ();

}//end Templatemethodpattern ()

public void Showtemplatemethodpattern ()

{

Square.drow ();

}//end Showtemplatemethodpattern ()

public static void Main (string[] args)

{

System.out.println ("The Template Method pattern!");

Templatemethodpattern tmp = new Templatemethodpattern ();

Tmp.showtemplatemethodpattern ();

}//end Main (...)

}//end class Templatemethodpattern

Run Result:

The Template method pattern!

Draw a square

The Template method pattern allows us to define the order of these steps first, and then overload those steps that need to be changed.

trackback:http://tb.blog.csdn.net/trackback.aspx?postid=954056  

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.