学妹惊呼:使用Java8改造后的模板方法模式真的是yyds
发布于 2021-09-04 18:19 ,所属分类:软件编程学习资料
(给程序员零距离加星标,了解项目开发.)
/**
*模板方法设计模式的抽象类
*@authorhollis
*/
publicabstractclassAbstractBusinessHandler{
/**
*模板方法
*/
publicfinalvoidexecute(){
getNumber();
handle();
judge();
}
/**
*取号
*@return
*/
privatevoidgetNumber(){
System.out.println("number-00"+RandomUtils.nextInt());
}
/**
*办理业务
*/
publicabstractvoidhandle();//抽象的办理业务方法,由子类实现
/**
*评价
*/
privatevoidjudge(){
System.out.println("giveapraised");
}
}
publicclassSaveMoneyHandlerextendsAbstractBusinessHandeler{
@Override
publicvoidhandle(){
System.out.println("save1000");
}
}
publicstaticvoidmain(String[]args){
SaveMoneyHandlersaveMoneyHandler=newSaveMoneyHandler();
saveMoneyHandler.execute();
}
number-00958442164
save1000
giveapraised
//取钱业务的实现类
publicclassDrawMoneyHandlerextendsAbstractBusinessHandeler{
@Override
publicvoidhandle(){
System.out.println("draw1000");
}
}
//理财业务的实现类
publicclassMoneyManageHandlerextendsAbstractBusinessHandeler{
@Override
publicvoidhandle(){
System.out.println("moneymanage");
}
}
publicList<String>getList(){
returnnewArrayList();
}
Supplier<List<String>>listSupplier=ArrayList::new;
publicvoidsum(Stringa1){
System.out.println(a1);
}
Consumer<String>printConsumer=a1->System.out.println(a1);
Consumer<String>stringConsumer=(s)->System.out.println(s.length());
Arrays.asList("ab","abc","a","abcd").stream().forEach(stringConsumer);
/**
*@authorHollis
*/
publicclassBankBusinessHandler{
privatevoidexecute(Consumer<BigDecimal>consumer){
getNumber();
consumer.accept(null);
judge();
}
privatevoidgetNumber(){
System.out.println("number-00"+RandomUtils.nextInt());
}
privatevoidjudge(){
System.out.println("giveapraised");
}
}
/**
*@authorHollis
*/
publicclassBankBusinessHandler{
publicvoidsave(BigDecimalamount){
execute(a->System.out.println("save"+amount));
}
}
publicstaticvoidmain(String[]args)throws{
BankBusinessHandlerbusinessHandler=newBankBusinessHandler();
businessHandler.save(newBigDecimal("1000"));
}
number-001736151440
save1000
giveapraised
/**
*@authorHollis
*/
publicclassBankBusinessHandler{
publicvoidsave(BigDecimalamount){
execute(a->System.out.println("save"+amount));
}
publicvoiddraw(BigDecimalamount){
execute(a->System.out.println("draw"+amount));
}
publicvoidmoneyManage(BigDecimalamount){
execute(a->System.out.println("draw"+amount));
}
}
/**
*模板方法
*/
protectedvoidexecute(Supplier<String>supplier,Consumer<BigDecimal>consumer){
Stringnumber=supplier.get();
System.out.println(number);
if(number.startsWith("vip")){
//Vip号分配到VIP柜台
System.out.println("AssignToVipCounter");
}
elseif(number.startsWith("reservation")){
//预约号分配到专属客户经理
System.out.println("AssignToExclusiveCustomerManager");
}else{
//默认分配到普通柜台
System.out.println("AssignToUsualManager");
}
consumer.accept(null);
judge();
}
publicclassBankBusinessHandlerextendsAbstractBusinessHandler{
publicvoidsaveVip(BigDecimalamount){
execute(()->"vipNumber-00"+RandomUtils.nextInt(),a->System.out.println("save"+amount));
}
publicvoidsave(BigDecimalamount){
execute(()->"number-00"+RandomUtils.nextInt(),a->System.out.println("save"+amount));
}
publicvoidsaveReservation(BigDecimalamount){
execute(()->"reservationNumber-00"+RandomUtils.nextInt(),a->System.out.println("save"+amount));
}
}
BankBusinessHandlerbusinessHandler=newBankBusinessHandler();
businessHandler.saveVip(newBigDecimal("1000"));
vipNumber-001638110566
AssignToVipCounter
save1000
giveapraised
文 章 精 选 1、灵魂画手:程序员不可怕,就怕程序员会画画 2、员工哭诉:没了6000一天的加班费,北京房贷还不起! 3、为赢过老婆打造自动瞄准弓,闭眼也能百发百中,零件电路算法全自制 4、《个人信息保护法》来了!明令禁止“大数据杀熟” 5、淘宝搞了个大事,支付宝可能不再是唯一了 6、一位19年的Mac用户:“我真的很讨厌库克” 7、笑到头掉哈哈哈哈!沙雕网站合集来一波 8、为了一个HTTPS,浏览器操碎了心···
相关资源