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

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

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

    JDK 設(shè)計(jì)模式 Pattern

    1. 策略模式  - strategy pattern
    /**
        Strategy Pattern 使用參數(shù)args[](regular expression 正則表達(dá)式)決定所要讀取的文件類型。 而不是在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) 評(píng)論(0)  編輯  收藏 所屬分類: J2EE

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

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲成av人片天堂网老年人| 亚洲人成网亚洲欧洲无码久久| 老牛精品亚洲成av人片| 久久精品国产亚洲精品| 18禁男女爽爽爽午夜网站免费| 亚洲色偷偷综合亚洲av78| 亚洲中文字幕日产乱码高清app| 久久成人国产精品免费软件| 视频一区在线免费观看| 久久亚洲精品成人777大小说| 成人毛片免费观看| 中国一级毛片视频免费看| 亚洲一区在线观看视频| 亚洲精品国产高清不卡在线| 18禁黄网站禁片免费观看不卡| 免费观看亚洲人成网站| 亚洲高清在线mv| 亚洲女人被黑人巨大进入| av大片在线无码免费| 亚洲免费日韩无码系列| 亚洲一区二区免费视频| 亚洲乱码无码永久不卡在线| 在线免费观看一级毛片| 99精品视频免费在线观看| 一级a性色生活片久久无少妇一级婬片免费放| 亚洲黄色片在线观看| 久久亚洲色一区二区三区| 全免费a级毛片免费**视频| 亚洲一区免费观看| 国产精品极品美女自在线观看免费| 亚洲成A人片在线播放器| 亚洲欧洲日韩不卡| 久久激情亚洲精品无码?V| 在线jyzzjyzz免费视频| 久久午夜夜伦鲁鲁片免费无码影视| 成人片黄网站色大片免费观看cn| 亚洲精品日韩一区二区小说| 亚洲图片激情小说| 亚洲精品自产拍在线观看动漫| 亚洲国产精品成人久久蜜臀| 全免费A级毛片免费看网站|