0

我在 github 上找到了以下代码:

public class DummySSLSocketFactory extends SSLSocketFactory {

    private static final Logger LOG = LoggerFactory.getLogger(DummySSLSocketFactory.class);
    private SSLSocketFactory factory;

    public DummySSLSocketFactory() {
        try {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            TrustManager[] trustManagers = new TrustManager[] {new DummyTrustManager()};
            sslContext.init(null, trustManagers, new java.security.SecureRandom());
            factory = sslContext.getSocketFactory();
        } catch (Exception e) {
            throw new RuntimeCamelException("Error creating DummySSLSocketFactory: " + e.getMessage(), e);
        }
    }

    /**
     * Must provide this getDefault operation for JavaMail to be able to use this factory.
     */
    public static SocketFactory getDefault() {
        LOG.warn("Camel is using DummySSLSocketFactory as SSLSocketFactory (only to be used for testing purpose)");
        return new DummySSLSocketFactory();
    }

    public String[] getDefaultCipherSuites() {
        return factory.getDefaultCipherSuites();
    }

    public String[] getSupportedCipherSuites() {
        return factory.getSupportedCipherSuites();
    }

    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
        return factory.createSocket(socket, host, port, autoClose);
    }

    public Socket createSocket(String host, int port) throws IOException {
        return factory.createSocket(host, port);
    }

    public Socket createSocket(String host, int port, InetAddress localAddress, int localPort)
        throws IOException {
        return factory.createSocket(host, port, localAddress, localPort);
    }

    public Socket createSocket(InetAddress host, int port) throws IOException {
        return factory.createSocket(host, port);
    }

    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
        throws IOException {
        return factory.createSocket(address, port, localAddress, localPort);
    }

    public Socket createSocket() throws IOException {
        // must have this createSocket method
        return factory.createSocket();
    }

}

类似的实现在不同的项目中被广泛使用。我试图弄清楚同时使用继承和组合的目的是什么。看起来只要有一个返回结果的工厂方法就足够了,sslContext.getSocketFactory();因为它根本不扩展类。

4

2 回答 2

1

我不知道谁编写这段代码的确切意图是什么,但是在这种类组织中很明显,使用组合和继承,DummySSLSocketFactory可以使用SSLSocketFactory中实现的受保护方法,甚至可以使用上面的类(使用 super )。 这是可能的,因为它引用了它的父类
DummySSLSocketFactorySSLSocketFactory

 private SSLSocketFactory factory;

在 DummySSLSocketFactory 的构造函数中设置

factory = sslContext.getSocketFactory();

如果没有此参考,您将能够使用 super 访问 SSLSocketLayer,但不能使用更高的类。

于 2017-11-20T17:38:35.430 回答
0

这是一种常见的模式(如您所述)。

子类可以扩展类(或实现接口)以建立“is-a”关系。在您的示例中, DummySSLSocketFactoryis-a SSLSocketFactory。“is-a”关系允许多态性。

子类可能不希望完全覆盖(或在接口的情况下实现)方法,因此它使用组合来获取已经覆盖了无趣但必需的方法的东西。为了实现这一点,子类将对“无趣”(我的术语)方法的调用转发给组合成员。在您的示例中,原始类SSLSocketFactory提供了该类不感兴趣的东西的“默认”实现DummySSLSocketFactory

将调用转发到组合成员是代理模式的一种变体。它不是一个完整的代理,因为它改变了功能。

于 2017-11-20T17:50:39.293 回答