My approach to OOP seems to be wrong - need to check the class

I have a piece of equipment that I will be connecting to and this connection could be TCP / IP or serial. I am writing a class that models configuration data, and one of the configuration parameters is information about that connection.

Serial has configuration options like CommPort, BaudRate, Parity, etc. A TCP / IP connection will have a host and port.

So, it seems obvious that I will have SerialConnectionInfo and TcpConnectionInfo classes. But these classes are members of the Equipment class.

It seems to me that there should be some kind of main class or interface ConnectionInfo, but the problem is that although the two classes are conceptually similar, they do not have common fields. So, something like the following doesn't seem to make any sense:

interface IConnectionInfo {
}

class SerialConnectionInfo : IConnectionInfo {
  string CommPort;
}

class TcpConnectionInfo : IConnectionInfo {
  string Host;
}

class Equipment {
  IConnectionInfo Connection;
}

      

I could do something like this, but then at some point I will definitely need to check which Connection class is and pass it to the appropriate class. It all smells bad. Anyone have any ideas?

+3


source to share


3 answers


You are using an interface that you are not interested in implementing. Thus, the interface must provide a method Connect()

that the classes implement.

The classes then contain the properties required for the given type of connection. Like this TcpConnection

:

public interface IConnection
{
    void Connect();
}

public class TcpConnection : IConnection
{
    public string Host { get; private set; }
    public int Port { get; private set; }

    private Socket _socket;

    public TcpConnection(string host, int port)
    {
        Host = host;
        Port = port;
    }

    public void Connect()
    {
        _socket = new Socket(...);
    }
}

      

Then you can create an instance:



IConnection connectionInfo = new TcpConnection("example.com", "1337");

      

And pass it as an interface, naming Connect()

on it to connect.

There is not enough information in your question on what classes, interface or properties should do, so I cannot model my answer to this question. Also, it looks more like something for http://programmers.stackexchange.com as it is about design, not implementation.

+6


source


If TcpConnectionInfo

u SerialConnectionInfo

have something in common, it doesn't make much sense. However, if this is the case, it would make more sense, for example:



interface IConnectionInfo {
    public string GetConnectionSocketDescription();
    public void Connect();
    // etc
}

class SerialConnectionInfo : IConnectionInfo {
  string CommPort;
  // make GetConnectionSocketDescription return CommPort
  // make Connect work for serial connection
}

class TcpConnectionInfo : IConnectionInfo {
  string Host;
  // make GetConnectionSocketDescription return Host
  // make Connect work for TCP connection
}

      

0


source


You should implement the adapter pattern and allow the client to connect to the server using TCP or Serialport.

0


source







All Articles