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

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产亚洲精品a在线观看app| 亚洲中文字幕无码av在线| 亚洲色婷婷一区二区三区| 亚洲成av人片在线看片| 色天使色婷婷在线影院亚洲| 99精品全国免费观看视频| 中文字幕亚洲一区| 亚洲欧美一区二区三区日产| 成人性生交视频免费观看| 亚洲人片在线观看天堂无码| 午夜一级毛片免费视频| 国产亚洲精品AAAA片APP| 国产一卡二卡≡卡四卡免费乱码| 怡红院亚洲红怡院在线观看| mm1313亚洲精品国产| 中文字幕在线免费视频| 亚洲国产国产综合一区首页| 一级特黄a大片免费| 日韩人妻无码免费视频一区二区三区| 亚洲黄色三级网站| 成年免费a级毛片免费看无码| 亚洲精品午夜国产VA久久成人 | 亚洲人成网站影音先锋播放| 91精品手机国产免费| 中文字幕精品亚洲无线码一区| 18禁超污无遮挡无码免费网站| 亚洲视频在线观看免费| 无码专区永久免费AV网站| 美女尿口扒开图片免费| 亚洲精品国产成人片| 波多野结衣在线免费视频| 国产91成人精品亚洲精品| 亚洲欧洲国产精品香蕉网| 4虎永免费最新永久免费地址| 亚洲AV女人18毛片水真多| 亚洲精品无码AV人在线播放| 毛片免费视频播放| 两个人看的www免费高清 | 夜夜亚洲天天久久| 日产乱码一卡二卡三免费| 久久国产免费观看精品|