扯淡设计模式2:java,模板模式,

模板模式:

package com.dayuanit.service;

public abstract class UserService {

    public void login(String userName, String pwd){
System.out.println("login begin"); first();
second();
third(); System.out.println("login over");
} public void first(){
System.out.println("login 1");
}
protected abstract void second();
public void third(){
System.out.println("login 3");
} }
package com.dayuanit.authen;

import com.dayuanit.service.UserService;

public class AlipayAuthen extends UserService{
@Override
protected void second(){
System.out.println("alipay login 2");
}
}
package com.dayuanit.authen;

import com.dayuanit.service.UserService;

public class WeiXinAuthen extends UserService{

    @Override
public void second(){
System.out.println("weixin login 2");
}
}
package com.dayuanit;

import com.dayuanit.authen.AlipayAuthen;
import com.dayuanit.authen.WeiXinAuthen; public class Test {
public static void main(String[] args) { //new UserService().login("tom", "111");
new WeiXinAuthen().login("tom","111");
System.out.println("--------------------------------");
new AlipayAuthen().login("tom", "111");
}
}

模板模式结束;

上一篇:jsp:forward response.sendRedirect


下一篇:[原创]cocos2d-x研习录-第三阶 背景音乐和音效