zoukankan      html  css  js  c++  java
  • 设计模式课程 设计模式精讲 5-3 工厂方法源码解析

    1    源码解析

    1.1  collection的iterator解析

    1.2  http协议类解析

    1    源码解析
    1.1  collection的iterator解析

    Collection:(collection相当于抽象工厂(这里的抽象工厂指代工厂的父类),iterator相当于抽象类,abstractList相当于工厂,Itr相当于实现类)

    Iterator<E> iterator();
    
        /**
         * Returns an array containing all of the elements in this collection.
         * If this collection makes any guarantees as to what order its elements
         * are returned by its iterator, this method must return the elements in
         * the same order.
         *
         * <p>The returned array will be "safe" in that no references to it are
         * maintained by this collection.  (In other words, this method must
         * allocate a new array even if this collection is backed by an array).
         * The caller is thus free to modify the returned array.
         *
         * <p>This method acts as bridge between array-based and collection-based
         * APIs.
         *
         * @return an array containing all of the elements in this collection
         */

    AbstractList

    /**
         * Returns an iterator over the elements in this list in proper sequence.
         *
         * <p>This implementation returns a straightforward implementation of the
         * iterator interface, relying on the backing list's {@code size()},
         * {@code get(int)}, and {@code remove(int)} methods.
         *
         * <p>Note that the iterator returned by this method will throw an
         * {@code UnsupportedOperationException} in response to its
         * {@code remove} method unless the list's {@code remove(int)} method is
         * overridden.
         *
         * <p>This implementation can be made to throw runtime exceptions in the
         * face of concurrent modification, as described in the specification
         * for the (protected) {@code modCount} field.
         *
         * @return an iterator over the elements in this list in proper sequence
         *
         * @see #modCount
         */
        public Iterator<E> iterator() {
        return new Itr();
        }

    private
    class Itr implements Iterator<E> {

    /** * Index of element to be returned by subsequent call to next. */ int cursor = 0; /** * Index of element returned by most recent call to next or * previous. Reset to -1 if this element is deleted by a call * to remove. */ int lastRet = -1; /** * The modCount value that the iterator believes that the backing * List should have. If this expectation is violated, the iterator * has detected concurrent modification. */ int expectedModCount = modCount; public boolean hasNext() { return cursor != size(); } public E next() { checkForComodification(); try { E next = get(cursor); lastRet = cursor++; return next; } catch (IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } } public void remove() { if (lastRet == -1) throw new IllegalStateException(); checkForComodification(); try { AbstractList.this.remove(lastRet); if (lastRet < cursor) cursor--; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }
    1.2  http协议类的解析

    urlstreamhandlerfactory类

    该类相当于创建工厂抽象类

    /*
     * %W% %E%
     *
     * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
     * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     */
    
    package java.net;
    
    /**
     * This interface defines a factory for <code>URL</code> stream
     * protocol handlers.
     * <p>
     * It is used by the <code>URL</code> class to create a
     * <code>URLStreamHandler</code> for a specific protocol.
     *
     * @author  Arthur van Hoff
     * @version %I%, %G%
     * @see     java.net.URL
     * @see     java.net.URLStreamHandler
     * @since   JDK1.0
     */
    public interface URLStreamHandlerFactory {
        /**
         * Creates a new <code>URLStreamHandler</code> instance with the specified
         * protocol.
         *
         * @param   protocol   the protocol ("<code>ftp</code>",
         *                     "<code>http</code>", "<code>nntp</code>", etc.).
         * @return  a <code>URLStreamHandler</code> for the specific protocol.
         * @see     java.net.URLStreamHandler
         */
        URLStreamHandler createURLStreamHandler(String protocol);
    }

    工厂实现类:

     private static class Factory implements URLStreamHandlerFactory {
            private static String PREFIX = "sun.net.www.protocol";
    
            private Factory() {
            }
    
            public URLStreamHandler createURLStreamHandler(String var1) {
                String var2 = PREFIX + "." + var1 + ".Handler";
    
                try {
                    Class var3 = Class.forName(var2);
                    return (URLStreamHandler)var3.newInstance();
                } catch (ClassNotFoundException var4) {
                    var4.printStackTrace();
                } catch (InstantiationException var5) {
                    var5.printStackTrace();
                } catch (IllegalAccessException var6) {
                    var6.printStackTrace();
                }
    
                throw new InternalError("could not load " + var1 + "system protocol handler");
            }
        }
    }

    launcher类

    URLStreamHandler 对应video类抽象产品,它的子类handler对应video实现类

     public URLStreamHandler createURLStreamHandler(String var1) {
                String var2 = PREFIX + "." + var1 + ".Handler";
    
                try {
                    Class var3 = Class.forName(var2);
                    return (URLStreamHandler)var3.newInstance();
                } catch (ClassNotFoundException var4) {
                    var4.printStackTrace();
                } catch (InstantiationException var5) {
                    var5.printStackTrace();
                } catch (IllegalAccessException var6) {
                    var6.printStackTrace();
                }
    
                throw new InternalError("could not load " + var1 + "system protocol handler");
            }

    handler(http) 子类

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package sun.net.www.protocol.http;
    
    import java.io.IOException;
    import java.net.Proxy;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLStreamHandler;
    
    public class Handler extends URLStreamHandler {
        protected String proxy;
        protected int proxyPort;
    
        protected int getDefaultPort() {
            return 80;
        }
    
        public Handler() {
            this.proxy = null;
            this.proxyPort = -1;
        }
    
        public Handler(String var1, int var2) {
            this.proxy = var1;
            this.proxyPort = var2;
        }
    
        protected URLConnection openConnection(URL var1) throws IOException {
            return this.openConnection(var1, (Proxy)null);
        }
    
        protected URLConnection openConnection(URL var1, Proxy var2) throws IOException {
            return new HttpURLConnection(var1, var2, this);
        }
    }
  • 相关阅读:
    商业模式--资源整合
    “不小心出卖了领导”
    计算机禁用桌面并且禁用键盘Win+*快捷键组合之后如何打开文件资源管理器
    spring-boot 参数长度、文件上传大小限制问题
    Python 学习基础
    Python字典详解
    Python元组
    Python列表详解
    Python变量类型
    Python字符串详解
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/11285027.html
Copyright © 2011-2022 走看看