1024programmer Java GOF23 template method pattern

GOF23 template method pattern

2019 Unicorn companies spend heavily to recruit Python engineers>>> hot3.png

174330_oGKK_2828556.png

174351_Y5hk_2828556.png

Code Example&# xff1a;

package com.gof.templateMethod;public abstract class BankTemplateMethod {public void takeNumber() {System.out.println("Take number queue&#xff01 ;");}public abstract void transact();//Hook method public void evaluate() {System.out.println("Feedback Rating");}/*** Template method*/public final void process() { this.takeNumber();this.transact();this.evaluate();}
}

package com. gof.templateMethod;public class Client {public static void main(String[] args) {
// DrawMoney drawMoney = new DrawMoney();
// drawMoney.process();// Use anonymous Internal class new BankTemplateMethod() {@Overridepublic void transact() {System.out.println("I want to save money");}}.process();}
}class DrawMoney extends BankTemplateMethod{&# 64;Overridepublic void transact() {System.out.println("I want to withdraw money!");}}

Console:

Queue to get a number!
I want to deposit money
Feedback rating

175531_OIk6_2828556.png

Redirect: https://my.oschina.net/captainliu/blog/903872

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/736979

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索