设计模式-结构型模式-模板方法
2011-05-04 16:40
323 查看
行为模式涉及到算法和对象间职责的分配。
template method:定一个操作中的算法骨架,而将些步骤延迟到子类当中,TemplateMethod使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
package behaviour.templatemethod;
/**
* An abstract class which can get content from a file or a HTTP URL
* or other resource
*/
public abstract class AbstractRead {
protected String resource;
public void getContent() { // Template Method
if(open()) {
readContent();
close();
}
}
public void setResource(String s) {
resource = s;
}
protected abstract boolean open();
protected abstract void readContent();
protected abstract void close();
}
package behaviour.templatemethod;
/**
* A concrete class extends AbstractRead
* This class can read from a file
*/
import java.io.*;
public class ReadFile extends AbstractRead {
private BufferedReader in = null;
public ReadFile() {
}
public ReadFile(String fileName) {
resource = fileName;
}
protected boolean open() {
try {
in = new BufferedReader(new FileReader(resource));
} catch(IOException e) {
System.out.println("Can not open file!");
return false;
}
return true;
}
protected void readContent() {
try {
if(in != null) {
String str;
while((str = in.readLine()) != null) {
System.out.println(str);
}
}
} catch(IOException e) {
System.out.println("Read file error !");
}
}
protected void close() {
if(in != null) {
try {
in.close();
} catch(IOException e) {
System.out.println("IO error !");
}
}
}
}
package behaviour.templatemethod;
/**
* A concrete class extends AbstractRead
* This class can read HTML from a HTTP URL
*/
import java.io.*;
import java.net.*;
public class ReadHtml extends AbstractRead {
private URLConnection conn;
private BufferedReader in;
public ReadHtml() {
}
public ReadHtml(String s) {
resource = s;
}
public boolean open() {
try {
URL url = new URL(resource);
conn = url.openConnection();
in = new BufferedReader (
new InputStreamReader(conn.getInputStream()));
} catch (MalformedURLException e) {
System.out.println("Uable to connect URL:" + resource);
return false;
} catch (IOException e) {
System.out.println("IOExeption when connecting to URL" + resource);
return false;
}
return true;
}
protected void readContent() {
try {
if(in != null) {
String str;
while((str = in.readLine()) != null) {
System.out.println(str);
}
}
} catch(IOException e) {
System.out.println("Read file error !");
}
}
protected void close() {
if(in != null) {
try {
in.close();
} catch(IOException e) {
System.out.println("IO error !");
}
}
}
}
package behaviour.templatemethod;
/**
* A test client
*/
public class Test {
public static void main(String[] args) {
// You should change the path of "test.txt" in your local machine
String fileName = "d://javaproject//TemplateMethod//src//test.txt";
String url = "http://www.the9.com/main.htm";
AbstractRead fileRead = new ReadFile();
AbstractRead htmlRead = new ReadHtml();
fileRead.setResource(fileName);
htmlRead.setResource(url);
System.out.println("----- Read from a file -----");
fileRead.getContent();
System.out.println("----- Read from a url -----");
htmlRead.getContent();
}
}
test.txt
A test file !
JAVA
Kava
c#
c++
...........
template method:定一个操作中的算法骨架,而将些步骤延迟到子类当中,TemplateMethod使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
package behaviour.templatemethod;
/**
* An abstract class which can get content from a file or a HTTP URL
* or other resource
*/
public abstract class AbstractRead {
protected String resource;
public void getContent() { // Template Method
if(open()) {
readContent();
close();
}
}
public void setResource(String s) {
resource = s;
}
protected abstract boolean open();
protected abstract void readContent();
protected abstract void close();
}
package behaviour.templatemethod;
/**
* A concrete class extends AbstractRead
* This class can read from a file
*/
import java.io.*;
public class ReadFile extends AbstractRead {
private BufferedReader in = null;
public ReadFile() {
}
public ReadFile(String fileName) {
resource = fileName;
}
protected boolean open() {
try {
in = new BufferedReader(new FileReader(resource));
} catch(IOException e) {
System.out.println("Can not open file!");
return false;
}
return true;
}
protected void readContent() {
try {
if(in != null) {
String str;
while((str = in.readLine()) != null) {
System.out.println(str);
}
}
} catch(IOException e) {
System.out.println("Read file error !");
}
}
protected void close() {
if(in != null) {
try {
in.close();
} catch(IOException e) {
System.out.println("IO error !");
}
}
}
}
package behaviour.templatemethod;
/**
* A concrete class extends AbstractRead
* This class can read HTML from a HTTP URL
*/
import java.io.*;
import java.net.*;
public class ReadHtml extends AbstractRead {
private URLConnection conn;
private BufferedReader in;
public ReadHtml() {
}
public ReadHtml(String s) {
resource = s;
}
public boolean open() {
try {
URL url = new URL(resource);
conn = url.openConnection();
in = new BufferedReader (
new InputStreamReader(conn.getInputStream()));
} catch (MalformedURLException e) {
System.out.println("Uable to connect URL:" + resource);
return false;
} catch (IOException e) {
System.out.println("IOExeption when connecting to URL" + resource);
return false;
}
return true;
}
protected void readContent() {
try {
if(in != null) {
String str;
while((str = in.readLine()) != null) {
System.out.println(str);
}
}
} catch(IOException e) {
System.out.println("Read file error !");
}
}
protected void close() {
if(in != null) {
try {
in.close();
} catch(IOException e) {
System.out.println("IO error !");
}
}
}
}
package behaviour.templatemethod;
/**
* A test client
*/
public class Test {
public static void main(String[] args) {
// You should change the path of "test.txt" in your local machine
String fileName = "d://javaproject//TemplateMethod//src//test.txt";
String url = "http://www.the9.com/main.htm";
AbstractRead fileRead = new ReadFile();
AbstractRead htmlRead = new ReadHtml();
fileRead.setResource(fileName);
htmlRead.setResource(url);
System.out.println("----- Read from a file -----");
fileRead.getContent();
System.out.println("----- Read from a url -----");
htmlRead.getContent();
}
}
test.txt
A test file !
JAVA
Kava
c#
c++
...........
相关文章推荐
- 设计模式C++实现(5)——原型模式、模板方法模式
- java设计模式 模板方法模式Template Method
- 跟我学设计模式视频教程——面向对象理论,模板方法
- 模板方法设计模式
- 基于设计模式的学习之旅-----模板方法模式(附源码)
- C# 设计模式之 模板方法
- 我所理解的设计模式(C++实现)——模板方法模式(Template Method Pattern)
- 模板方法设计模式
- SPRING与设计模式--策略模式与模板方法模式
- 设计模式 - 模板方法模式(template method pattern) JFrame 详解
- 设计模式之模板方法
- 设计模式----模板方法模式(Template Method)
- IOS设计模式学习(18)模板方法
- 【java设计模式】之 模板方法(Template Method)模式
- 【设计模式】—-(13)模板方法模式(行为型)
- java(2) 设计模式-模板方法设计模式
- 深入理解JavaScript系列(41):设计模式之模板方法详解
- 设计模式系列(十二)模板方法模式(Template Method Pattern)
- Java设计模式——模板方法
- 三分钟理解“模板方法模式”——设计模式轻松掌握