SocketFactory、DefaultSocketFactory、ServerSocketFactory、DefaultServerSocketFactory

  SocketFactory是一个抽象类(抽象类不能被实例化,但可以有构造函数)。

  描述:This class creates sockets./此类用于创建sockets

public abstract class SocketFactory {
    private static SocketFactory theFactory;
  //返回环境默认的socket工厂的副本。
public static SocketFactory getDefault() { synchronized (SocketFactory.class) { if (theFactory == null) { theFactory = new DefaultSocketFactory(); } } return theFactory; }
  //创建一个无连接的socket(抛出SocketException("Unconnected sockets not implemented")异常)
public Socket createSocket() throws IOException { UnsupportedOperationException localUnsupportedOperationException = new UnsupportedOperationException(); SocketException localSocketException = new SocketException("Unconnected sockets not implemented"); localSocketException.initCause(localUnsupportedOperationException); throw localSocketException; }   //创建一个套接字并将其连接到指定地址的指定端口号。 public abstract Socket createSocket(String host, int port) throws IOException, UnknownHostException;   //创建套接字并将其连接到指定远程主机的指定远程端口号。 public abstract Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException;   //创建一个套接字并将其连接到指定地址的指定端口号。 public abstract Socket createSocket(InetAddress address, int port) throws IOException;   //创建套接字并将其连接到指定远程地址上的指定远程端口。 public abstract Socket createSocket(InetAddress address, int port, InetAddress localAdress,int localPort) throws IOException; }

  DefaultSocketFactory是SocketFactory的子类。socket创建的默认工厂类

class DefaultSocketFactory extends SocketFactory {
    public Socket createSocket() {
        return new Socket();
    }

    public Socket createSocket(String paramString, int paramInt) throws IOException, UnknownHostException {
        return new Socket(paramString, paramInt);
    }

    public Socket createSocket(InetAddress paramInetAddress, int paramInt) throws IOException {
        return new Socket(paramInetAddress, paramInt);
    }

    public Socket createSocket(String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2)
            throws IOException, UnknownHostException {
        return new Socket(paramString, paramInt1, paramInetAddress, paramInt2);
    }

    public Socket createSocket(InetAddress paramInetAddress1, int paramInt1, InetAddress paramInetAddress2,
            int paramInt2) throws IOException {
        return new Socket(paramInetAddress1, paramInt1, paramInetAddress2, paramInt2);
    }
}

 

  ServerSocketFactory是一个抽象类(抽象类不能被实例化,但可以有构造函数)。

  描述:This class creates server sockets./此类用于创建服务器套间字

 

public abstract class ServerSocketFactory {
    private static ServerSocketFactory theFactory;
    //返回环境默认的socket工厂副本
    public static ServerSocketFactory getDefault() {
        synchronized (ServerSocketFactory.class) {
            if (theFactory == null) {
                theFactory = new DefaultServerSocketFactory();
            }
        }

        return theFactory;
    }
    //返回一个未绑定的服务器套间字
    public ServerSocket createServerSocket() throws IOException {
        throw new SocketException("Unbound server sockets not implemented");
    }
    //返回一个绑定到指定端口的服务器套间字
    public abstract ServerSocket createServerSocket(int port) throws IOException;
    //返回一个绑定到指定端口的服务器套间字,并指定backlog参数
    public abstract ServerSocket createServerSocket(int port, int backlog) throws IOException;
    //返回一个绑定到指定端口的服务间套间字,并指定backlog和ip地址
    public abstract ServerSocket createServerSocket(int port, int backlog, InetAddress adress)
            throws IOException;
}                

  DefaultServerSocketFactory是ServerSocketFactory的子类。ServerSocket创建的默认工厂类。

class DefaultServerSocketFactory extends ServerSocketFactory {
    public ServerSocket createServerSocket() throws IOException {
        return new ServerSocket();
    }

    public ServerSocket createServerSocket(int paramInt) throws IOException {
        return new ServerSocket(paramInt);
    }

    public ServerSocket createServerSocket(int paramInt1, int paramInt2) throws IOException {
        return new ServerSocket(paramInt1, paramInt2);
    }

    public ServerSocket createServerSocket(int paramInt1, int paramInt2, InetAddress paramInetAddress)
            throws IOException {
        return new ServerSocket(paramInt1, paramInt2, paramInetAddress);
    }
}

  简单测试

class TestFactory extends SocketFactory {

    @Override
    public Socket createSocket(String paramString, int paramInt) throws IOException, UnknownHostException {
    return null;
    }

    @Override
    public Socket createSocket(String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2)
        throws IOException, UnknownHostException {
    return null;
    }

    @Override
    public Socket createSocket(InetAddress paramInetAddress, int paramInt) throws IOException {
    return null;
    }

    @Override
    public Socket createSocket(InetAddress paramInetAddress1, int paramInt1, InetAddress paramInetAddress2,
        int paramInt2) throws IOException {
    return null;
    }

    public static void main(String[] args) throws UnknownHostException, IOException {
    TestFactory factory = new TestFactory();
    Socket socket1 = factory.createSocket("host", 1);
    System.out.println(socket1 == null);

    SocketFactory factory1 =  SocketFactory.getDefault();
    Socket socket2 = factory.createSocket("host", 1);
    System.out.println(socket2 == null);

    }
}

返回结果:

true

false

 

 

posted @ 2018-01-30 15:02  maxudong  阅读(2822)  评论(0编辑  收藏  举报