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

行为模式涉及到算法和对象间职责的分配。
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++
...........

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值