JAVA learning notes (13)-Template method mode template, learning notes Template

Source: Internet
Author: User

JAVA learning notes (13)-Template method mode template, learning notes Template
Template Method mode Template

/** Function: sorting and printing arrays ** definition: defines an algorithm (function) framework in the abstract class, and delays the specific steps in the subclass. * Composition: an abstract class, one or more implementation Classes * the abstract class contains: * 1. abstract method: defines only the specification and is implemented by sub-classes * 2. the template method is implemented by the parent class and the abstract method is called to complete the logic function. It is generally defined as a final method and does not allow subclass rewriting. ** advantages: * 1. scalability: add different implementation classes for extension * 2. ease of maintenance ** application: multiple sub-classes have the same method, and the logical functions of these methods are the same, we recommend that you use the template method mode */public abstract class Test03 {/** abstract method to sort arrays in ascending order */public abstract void sort (int [] nums ); /** template method: print the sorted array */public final void print (int [] nums) {this. sort (nums); // sorts the array System. out. println ("sorted array:"); for (int I = 0; I <nums. length; I ++) {System. out. print (nums [I] + "");}}}

Test class

/** Test class */public class Test {public static void main (String [] args) {int [] nums = {12, 45, 2,130, 43, 76, 3, 56}; Test03 test = new SubTest03 (); test. print (nums );}}

Inherited from Test03

/** Inherited from Test03 */public class SubTest03 extends Test03 {// override the sort method of the parent class public void sort (int [] nums) {for (int I = 0; I <nums. length-1; I ++) {for (int j = 0; j <nums. length-1-I; j ++) {if (nums [j] <nums [j + 1]) {int temp = nums [j]; nums [j] = nums [j + 1]; nums [j + 1] = temp ;}}}}}
Template Method Instance
/** Use mode and method mode to calculate the Volume */public class Test04 {public static void main (String [] args) {Volume v = new Cylinder (5.3, 4 ); system. out. println ("volume of the cylinder:" + v. getVolume (); v = new Cuboid (5, 3.2, 6.4); System. out. println ("cube volume:" + v. getVolume () ;}}/** Volume abstract class */abstract class Volume {private double height; // high public Volume (double height) {this. height = height;} public double getHeight () {return height;} // abstract method: calculate the area public abstract double getArea (); // template method: calculation Volume public double getVolume () {return getArea () * height ;}/ ** Cylinder class */class Cylinder extends Volume {private double r; // radius public Cylinder (double height, double r) {super (height); this. r = r ;}// Override the method of the parent class @ Override public double getArea () {return Math. PI * r; // calculate the area of the circle}/** Cuboid */class Cuboid extends Volume {private double length; // long private double width; // width public Cuboid (double height, double length, double width) {super (height); this. length = length; this. width = width;} // Override the method of the parent class @ Override public double getArea () {return length * width; // calculate the area of the rectangle }}

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.