<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)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产免费黄色大片| 深夜国产福利99亚洲视频| 久久大香香蕉国产免费网站| 美女视频黄a视频全免费网站色窝 美女被cao网站免费看在线看 | 久久精品国产亚洲精品2020| 久久亚洲精品无码aⅴ大香| 亚洲综合色一区二区三区| 在线观看亚洲免费视频| 亚洲黄片手机免费观看| 2019中文字幕在线电影免费 | 日韩欧美亚洲中文乱码| 黄色网址在线免费| 久久久久亚洲AV成人无码| 亚洲色成人网站WWW永久四虎| 久久久久免费看黄A片APP| 亚洲日韩在线观看免费视频| 久久精品国产亚洲av麻豆小说| 99精品视频在线观看免费播放| 免费播放特黄特色毛片| 亚洲制服丝袜中文字幕| 91成人免费观看在线观看| 成人免费视频小说| 亚洲综合一区二区精品久久| 一级女人18片毛片免费视频| 最近免费中文字幕4| 青青视频免费在线| 在线成人a毛片免费播放| 久久精品夜色国产亚洲av| 国产亚洲漂亮白嫩美女在线| 国产福利电影一区二区三区,亚洲国模精品一区| jizz免费观看| 亚洲一区二区三区四区在线观看 | 人妻无码久久一区二区三区免费| 中文字幕亚洲综合小综合在线| 又粗又黄又猛又爽大片免费| 国产无遮挡裸体免费视频在线观看 | 亚洲国产无套无码av电影| 亚洲欧美乱色情图片| 少妇高潮太爽了在线观看免费| 久久亚洲AV无码精品色午夜麻| 免费A级毛片无码免费视|