您的位置:首页 > 其它

设计模式-结构型模式-模板方法

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++
...........
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: