Java Template Method Design patterns (Understanding code principles from a real-life perspective)

Source: Internet
Author: User

Overview:

Define a function of the framework (skeleton), part of the function is determined, part of the function is undefined, the first part of the implementation of the deterministic, the indeterminate part of the delay to the implementation of subclasses.

Key steps to implement this pattern:

The first step: abstract classes, implementing template methods, defining the framework of functions (deterministic and indeterminate).

The second step: the concrete class, realizes the abstract method in the abstract class, has completed the complete function.

The usual, recently wanted to complete a BMW1 department, went to the 4S shop, the store happens to have a car, but the color I do not like, then the service consultant came up to explain: "Sir, you fancy this car on this model, appearance, engine, interior and other configurations are the same, but the color can be customized, So you don't have to worry about this. " Here we should understand, this is a typical template method, the determination of the function is the engine, appearance, interior, etc., is not sure is the color. Then we started the code implementation, after all, as a programmer, only to knock the code to buy a BMW AH:

First step: abstract class, defining functional framework
1 Abstract classbuycar{2      Public voidBuy () {3         //identified features4System.out.println ("Welcome to the BMW 4S shop, I'll introduce you to the car you chose");5System.out.println ("Appearance: Hatchback, family-style double kidney grid, typical BMW face");6System.out.println ("Engine: 2.0L Twin Vortex tube single turbo engine");7System.out.println ("Interior: .....") ");8System.out.println ("This car overall look like this, but the color can choose, you want white or red?") ");9         //Indeterminate feature OptionsTen Choosecolor (); One     } A     //defining an abstract method for an indeterminate function for a specific class implementation -      Public Abstract voidChoosecolor (); -}
Step Two: Concrete classes to implement abstract methods
1 class extends buycar{2      Public  void   Choosecolor () {3         System.out.println ("white"); 4     }5 }
Test class:
1  Public class templatemethod{2      Public Static void Main (string[] args) {3         New Man (); 4         m.buy (); 5     }6 }
Output Result:
1 Welcome to the BMW 4S shop, I'll introduce you to the car you chose 2 appearance: Hatchback, family-style double kidney grid, typical BMW face 3 Engine: 2. 0L Double Vortex tube single turbocharged engine 4 interior: ...  5 This car as a whole please look at this, but the color can choose, you want white or red it?  6 White

In this way, my car-buying journey is over. Also has a new understanding of the template method:

The model has his merits , such as:

(1) Template method mode removes duplicate code from subclasses by moving the invariant behavior to the superclass.

(2) Subclasses implement some of the details of the algorithm, which helps to extend the algorithm.

(3) through a parent class call the subclass implementation of the operation, through the subclass extension to add new behavior, in line with the "open-closed principle."

But the disadvantage is equally obvious: each different implementation needs to define a subclass, which causes the number of classes to increase and the design to be more abstract.

Java Template Method Design patterns (Understanding code principles from a real-life perspective)

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.