<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JDK 設計模式 Pattern

    1. 策略模式  - strategy pattern
    /**
        Strategy Pattern 使用參數args[](regular expression 正則表達式)決定所要讀取的文件類型。 而不是在code中直接hard code。
    */
    public class DirList3 {
        
    public static void main(final String[] args) {
            File path 
    = new File("D:\\Users\\wpeng\\workspace\\Hello\\src\\think\\in\\java\\io");
            String[] list;
            
    if(args.length == 0){
                list 
    = path.list();
            }
    else{
                list 
    = path.list(new FilenameFilter() {
                    
    private Pattern pattern = Pattern.compile(args[0]);
                    @Override
                    
    public boolean accept(File dir, String name) {
                        
    return pattern.matcher(name).matches();
                    }
                });
            }
            Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
            
    for(String dirItem : list){
                System.out.println(dirItem);
            }
        }
    }

    2. 裝飾者模式 - Decorator Pattern
    /**
     * 裝飾者模式 decorator pattern
     * FileInputStearm -> FileRader -> BufferReader
     * 
    @author WPeng
     * 
    @since 2012-11-5
     
    */
    public class BufferedInputFile {
        
    // Throw exceptions to console
        public static String read(String filename) throws IOException{
            
    // Reading input by lines
            BufferedReader in = new BufferedReader(new FileReader(filename));
            String s;
            StringBuilder sb 
    = new StringBuilder();
            
    while((s = in.readLine()) != null){
                sb.append(s 
    + "\n");
            }
            in.close();
            
    return sb.toString();
        }
        
    public static void main(String[] args) throws IOException {
            System.out.print(read(
    "BufferedInputFile.java"));

        // FileInputStream -> BufferedInputStream -> DataInputStream
        DataInputStream in = 
                
    new DataInputStream(
                        
    new BufferedInputStream(
                                
    new FileInputStream("FormattedMemoryInput.java")));
            
    while(true){
                System.out.print((
    char)in.readByte());
            }
        }
    }
    3. 模板模式 Template pattern
    public class MappedIO {
        
    private static int numOfInts = 4000000;
        
    private static int numOfUbuffInts = 200000;
        
        
    private abstract static class Tester{
            
    private String name;
            
    public Tester(String name){
                
    this.name = name;
            }
            
            
    public void runTest(){
                System.out.print(name 
    + "");
                
    try {
                    
    long start = System.nanoTime();
                    test();
                    
    double duration = System.nanoTime() - start;
                    System.out.format(
    "%.2f\n", duration/1.0e9);
                } 
    catch (IOException e) {
                    
    throw new RuntimeException(e);
                }
            }
            
            
    public abstract void test() throws IOException;
        }
        
        
    private static Tester[] tests = {
            
    new Tester("Stream Write") {
                @Override
                
    public void test() throws IOException {
                    DataOutputStream dos 
    = new DataOutputStream(
                            
    new BufferedOutputStream(
                                    
    new FileOutputStream(new File("temp.tmp"))));
                    
    for(int i=0; i<numOfInts; i++){
                        dos.writeInt(i);
                    }
                    dos.close();
                }
            },
            
    new Tester("Mapped Write") {
                @Override
                
    public void test() throws IOException {
                    FileChannel fc 
    = new RandomAccessFile("temp.tmp""rw").getChannel();
                    IntBuffer ib 
    = fc.map(
                            FileChannel.MapMode.READ_WRITE, 
    0, fc.size()).asIntBuffer();
                    
    for(int i=0; i<numOfInts; i++){
                        ib.put(i);
                    }
                    fc.close();
                }
            }

        };

        
    public static void main(String[] args) {
            
    for(Tester test : tests){
                test.runTest();
            }
        }
    }











    posted on 2012-11-05 09:26 鹽城小土包 閱讀(182) 評論(0)  編輯  收藏 所屬分類: J2EE

    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久久久亚洲AV片无码| 蜜桃视频在线观看免费网址入口| 国产午夜亚洲精品| 57pao国产成视频免费播放| 成年人免费观看视频网站| 亚洲一区无码中文字幕乱码| 99在线视频免费观看视频 | 亚洲精品人成电影网| 亚洲色www永久网站| 亚洲A∨精品一区二区三区下载| 一区二区三区在线免费观看视频| 免费一级国产生活片| 亚洲性一级理论片在线观看| 亚洲美女视频免费| 亚洲乱码中文论理电影| 天天摸夜夜摸成人免费视频| 美女又黄又免费的视频| 亚洲视频在线免费| 亚洲精品日韩一区二区小说| 国产网站免费观看| eeuss免费天堂影院| 亚洲av之男人的天堂网站| 免费VA在线观看无码| 日本免费网址大全在线观看| 亚洲中文无码永久免| 四虎永久免费影院| 在线观看特色大片免费网站| 亚洲精品91在线| 无码国模国产在线观看免费| 久久久久久毛片免费看| 亚洲午夜视频在线观看| 在线免费观看污网站| 成人免费777777被爆出| 免费国产综合视频在线看| 最近免费中文字幕中文高清| 亚洲另类古典武侠| 又大又黄又粗又爽的免费视频| 久久精品成人免费观看| 自拍偷自拍亚洲精品偷一| 亚洲成av人片在线观看无码不卡| 成人片黄网站色大片免费|