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)