行为型
public abstract class ICourse {
protected final void makeCourse() {
this.makePPT();
this.makeVideo();
if (this.isNeedArticle()){
this.makeArticle();
}
this.packageCourse();
}
final void makePPT() {
System.out.println("制作PPT");
}
final void makeVideo() {
System.out.println("制作视频");
}
final void makeArticle() {
System.out.println("制作手记");
}
protected Boolean isNeedArticle() {
return false;
};
abstract void packageCourse();
}
public class JavaCourse extends ICourse{
@Override
void packageCourse() {
System.out.println("JAVA 课程");
}
@Override
protected Boolean isNeedArticle() {
return true;
};
}
public class JavaScriptCourse extends ICourse{
@Override
void packageCourse() {
System.out.println("javaScript 课程");
}
}
public class Test {
public static void main(String[] args) {
System.out.println("制作JAVA课程开始 start");
ICourse course = new JavaCourse();
course.makeCourse();
System.out.println("制作JAVA课程开始 end");
System.out.println("制作JAVASCROIPT课程开始 start");
ICourse javaCourse = new JavaScriptCourse();
javaCourse.makeCourse();
System.out.println("制作JAVASCROIPT课程开始 end");
}
}
模板方法起到保护作用,抽象保护的方法,已到达模板条件不一样的想过
abstract public E get(int index);
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String msg = lStrings.getString("http.method_get_not_supported");
this.sendMethodNotAllowed(req, resp, msg);
}
protected void closeStatement(Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException var3) {
}
}
}