Template mode for Java design patterns

Source: Internet
Author: User

Objective:

Recently learned the glide open-source picture cache framework, learning to use Modelloader custom data source, glide clever use of Java template mode to expose the different URL data source, today to learn to summarize the template mode.

Template mode:

The template method pattern is the behavior pattern of the class. Prepare an abstract class, implement some of the logic in the form of concrete methods and concrete constructors, and then declare some abstract methods to force subclasses 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.

The Role abstract class (AbstractClass) in the pattern: the template method is implemented, and the skeleton of the algorithm is defined. Concrete Class (Concreteclass): Implements the abstract method in the abstract class, has completed the complete algorithm. The pattern pattern shows an example:

The above can we learn template mode motivation from glide, then we have to simulate glide define different data source templates to illustrate

1.) First define download abstract class Abstractimageloader
 Public Abstract classAbstractimageloader {//abstract class defines the entire process skeleton     Public Final voidDownloadimage (String ImageUrl,intWidthintheight) {        //get the final data source URL firstString finalimageurl=GetUrl (imageurl,width,height); //and then start doing the download    }    //The following are specific steps for different subclasses to complete according to their own characteristics    protected AbstractString GetUrl (String imageUrl,intWidthintheight);}

2.) Specific class implementation

Webpimageloader

 Public class extends   abstractimageloader {    @Override    protectedintint  height) {         return String.Format ("%s?imageview2/1/w/%d/h/%d/format/webp", imageUrl, width, height);    }}

Jpgimageloader

 Public class extends   abstractimageloader {    @Override    protectedintint  height) {         return String.Format ("%s?imageview2/1/w/%d/h/%d/format/jpg", imageUrl, width, height);    }}
3.) Specific Use
String imageUrl = "Http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg"=new   Webpimageloader (); Loader.downloadimage (IMAGEURL,200,200);
Pattern mode Advantages and disadvantages: 1.) Advantages The template method pattern removes duplicate code from subclasses by moving the invariant behavior to the superclass. Subclasses implement some of the details of the algorithm, which facilitates the expansion of the algorithm. By invoking the subclass implementation of a parent class, the new behavior is added through the subclass extension, which conforms to the open-close principle. 2.) Disadvantages each of the different implementations needs to define a subclass, which causes the number of classes to increase and the design to be more abstract. 3.) The application scenario in some classes of algorithms, using the same method, resulting in code duplication. Control subclass extensions, subclasses must adhere to algorithmic rules. Summary: Simple summary of the template model, today is also the last blog before the Dragon Boat Festival, Happy Dragon Boat Festival.

Template mode for Java design patterns

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.