Template method Mode (think in Java design mode)

Source: Internet
Author: User

Template method Pattern: The template method pattern is a modular design pattern as the name implies. Divide one behavior into multiple behaviors and flexibly invoke different modules to work. For example, eat in a restaurant, divided into: a la carte, eat, pay and other behavior no matter what hotel is roughly the three kinds of behavior. So we can build an abstract class restaurant to eat, and then according to different restaurants to modify the food to achieve the effect of this step. He is a kind of behavioral pattern.

 PackageCom.hhdeng.controller; Public classTest { Public Static voidMain (string[] args) {Havedinner HD=NewHavedinner (); hd. Haveeatdinner (NewChinesefood ()); hd. Haveeatdinner (NewJapanfood ()); }        }classhavedinner{ Public voidHaveeatdinner (Dinner D) {d.book ();        D.eat ();    D.pay (); }}Abstract classdinner{ Public Abstract voideat ();  Public  voidBook () {System.out.println ("Order a la carte"); }     Public  voidPay () {System.out.println ("Order Payment"); }}classJapanfoodextendsdinner{@Override Public voideat () {System.out.println ("Eat sushi, eat fish"); }}classChinesefoodextendsdinner{@Override Public voideat () {System.out.println ("Eat rice, eat mapo tofu"); }}

Template method Mode (think in Java design mode)

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.