<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 鹽城小土包 閱讀(176) 評論(0)  編輯  收藏 所屬分類: J2EE

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲精品成人久久| 无码专区—VA亚洲V天堂| 中文字幕亚洲情99在线| 最近中文字幕大全免费视频 | 中文在线免费不卡视频| 久久精品国产亚洲AV麻豆王友容 | 一边摸一边桶一边脱免费视频 | 在线免费观看一级毛片| 中文字幕亚洲精品无码| 好爽好紧好大的免费视频国产| 在线观看亚洲AV每日更新无码 | 日韩精品电影一区亚洲| selaoban在线视频免费精品| 亚洲性猛交XXXX| 免费一级毛片无毒不卡| 亚洲精品视频在线播放| 特级做A爰片毛片免费69 | 免费无码AV电影在线观看| 亚洲精品无码专区| 亚洲av高清在线观看一区二区| 国产久爱免费精品视频| 久久噜噜噜久久亚洲va久| 黄瓜视频影院在线观看免费| 亚洲乱人伦中文字幕无码| 全亚洲最新黄色特级网站| 永久免费A∨片在线观看| 亚洲精品欧洲精品| 国产精品自在自线免费观看| 亚欧洲精品在线视频免费观看| 久久久久久a亚洲欧洲AV| 亚洲中文无码永久免费| 人妻18毛片a级毛片免费看| 亚洲av无码不卡| 日韩毛片无码永久免费看| 一级毛片在线完整免费观看| 亚洲无线电影官网| 免费无码又爽又刺激高潮的视频| 久久精品无码免费不卡| 亚洲中文久久精品无码1| 亚洲精品WWW久久久久久| 噼里啪啦免费观看高清动漫4|