首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

设计形式之模板模式(Template Model)

2013-10-15 
设计模式之模板模式(Template Model)模板模式即设定一个模板类,执行的子类继承这个抽象类,实现具体的方法。

设计模式之模板模式(Template Model)

模板模式即设定一个模板类,执行的子类继承这个抽象类,实现具体的方法。用于不同实现类拥有相同的执行方法模式,只是内容不一样,但是执行的步骤是完全一样的,这也就是为什么可以用抽象类作为父类。

代码如下:

package TemplatedMethod;


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 TemplatedMethod;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


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 TemplatedMethod;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


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 !");
        }
    }
@Override
protected void close() {
// TODO Auto-generated method stub

}


}


package TemplatedMethod;


public class Test {
public static void main(String[] args) {
        // You should change the path of "test.txt" in your local machine
        String fileName = "D:\\项目参考资料\\book\\java\\23 design model\\templatemethod.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();
    }
}


输出:


-----  Read from a file  -----
/**
 *  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();
}


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 !");
            }
        }
    }
}


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 !");
        }


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();
    }
}
-----  Read from a url  -----
<html><frameset> <frame id='top' src="http://172.16.100.250/disable/disable.htm"><noframes> <body>Your browse does not support frame!</body> </noframes> </frameset> </html>

热点排行