Java設計模式之模板方法(Template Method)
阿新 • • 發佈:2018-11-12
模式定義
模板方法: 定義一個操作中的演算法的骨架,而將一些步驟延遲到子類中,模板方法使得子類可以不改變一個演算法的結構即可重定義該演算法的某些特定步驟。
注意:工廠方法及抽象工廠方法著重抽象的是產品,而模板方法著重抽象的是步驟。
需求背景
在實際開發過程中,經常遇到各種格式的資料之間的轉換,如 json轉java bean, XML轉java bean,YAML轉java bean, 這個時候模板模式是很好的選擇。
具體實現
- 生成轉換工具類模板
public abstract class Formatter {
public String formatBook(Book book, int format) {
beforeFormat();
String result = formating(book);
afterFormat();
return result;
}
protected void beforeFormat() {
System.out .println("format begins");
}
protected abstract String formating(Book book);
protected void afterFormat() {
System.out.println("format finished");
}
}
你會發現format_book方法只有四步,第一步呼叫before_format,去列印格式轉換前的日誌。第二步呼叫formating,這個方法是個抽象方法,用於處理具體的轉換邏輯,因此每一個繼承自Formatter的子類都需要重寫此方法,來實現各自的轉換邏輯。第三步呼叫after_format,去列印格式轉換後的日誌。第四步返回result。由於類中存在了抽象方法,我們也就需要把Formatter宣告成抽象類。
然後要定義專門的子類來處理每種傳輸格式的具體邏輯,這樣不同傳輸格式的邏輯可以從一個方法裡分離開,明顯便於閱讀和理解。
- XMLFormatter 繼承Formatter 生成具體的轉換工具
public class XMLFormatter extends Formatter {
@Override
protected String formating(Book book) {
String result = "";
result += "<book_name>" + book.getBookName() + "</book_name>\n";
result += "<pages>" + book.getPages() + "</pages>\n";
result += "<price>" + book.getPrice() + "</price>\n";
result += "<author>" + book.getAuthor() + "</author>\n";
result += "<isbn>" + book.getIsbn() + "</isbn>\n";
return result;
}
}
- JSONFormatter繼承Formatter生成具體的轉換工具
public class JSONFormatter extends Formatter {
@Override
protected String formating(Book book) {
String result = "";
result += "{\n";
result += "\"book_name\" : \"" + book.getBookName() + "\",\n";
result += "\"pages\" : \"" + book.getPages() + "\",\n";
result += "\"price\" : \"" + book.getPrice() + "\",\n";
result += "\"author\" : \"" + book.getAuthor() + "\",\n";
result += "\"isbn\" : \"" + book.getIsbn() + "\",\n";
result += "}";
return result;
}
}
- YAMLFormatter繼承Formatter生成具體的轉換工具
public class YAMLFormatter extends Formatter {
@Override
protected String formating(Book book) {
String result = "";
result += "book_name: " + book.getBookName() + "\n";
result += "pages: " + book.getPages() + "\n";
result += "price: " + book.getPrice() + "\n";
result += "author: " + book.getAuthor() + "\n";
result += "isbn: " + book.getIsbn() + "\n";
return result;
}
}
- 測試
public class Test {
public static void main(String[] args) throws Exception {
Book book = new Book();
book.setBookName("Thinking in Java");
book.setPages(880);
book.setPrice(68);
book.setAuthor("Bruce Eckel");
book.setIsbn("9787111213826");
XMLFormatter xmlFormatter = new XMLFormatter();
String result = xmlFormatter.formatBook(book);
System.out.println(result);
JSONFormatter jsonFormatter = new JSONFormatter();
result = jsonFormatter.formatBook(book);
System.out.println(result);
YAMLFormatter yamlFormatter = new YAMLFormatter();
String result = yamlFormatter.formatBook(book);
System.out.println(result);
}
}
參考連結:
https://blog.csdn.net/cdl2008sky/article/details/3335385
http://www.cnblogs.com/maowang1991/archive/2013/04/15/3023236.html