我想要一个"connectionList“,其中每个元素都存储不同的连接类型(TCP,R232)。我为"Connect“和"Disconnect”创建了一个抽象接口,但是由于"Config“需要每个类中的不同参数,所以不可能将其实现到接口中(或者是吗?)。
我做了个小小的想象:

我的解决方案之一是在调用((TCP)connectionList).Config()之前进行强制转换,但这意味着我必须尝试所有可能的类类型(TCP,RS232,.)例如,如果我想配置“连接3”。
我相信有比这个更好的解决方案。
代码示例:
List<IConnection> connectionList = new List<IConnection>();
connectionList.Add(new TCP());
connectionList.Add(new RS232());
connectionList[0].Connect();
connectionList[1].Connect(); // Works
connectionList[0].Config(); //Does not work because not in Interface
public abstract class IConnection
{
public abstract void Connect();
}
public class RS232 : IConnection
{
private int _baudRate;
public void Config(int baudRate) //Diffrent parameters then in TCP
{
_baudRate = baudRate;
}
public override void Connect()
{
Console.WriteLine("RS232 connect()");
}
}
public class TCP : IConnection
{
private int _ipAdress;
public void Config(int ipAdress) //Diffrent parameters then in RS232
{
_ipAdress = ipAdress;
}
public override void Connect()
{
Console.WriteLine("TCP Connect()");
}
}发布于 2022-10-19 10:12:30
我认为您需要一些接口,也许需要抽象的基本连接:
public interface ICanConnect
{
public void Connect();
public void Disconnect();
}
public interface IHaveConfig
{
public IConnectionConfig GetConfig();
}
public interface IConnectionConfig
{
}
public interface ITcpConfiguration: IConnectionConfig
{
public string IpAdress { get; set; }
}
// TODO add class
public interface IRS232Configuration : IConnectionConfig
{
public int BaudRate { get; set; }
}
// TODO add class应该在使用连接之前完成配置:
public abstract class BaseConnection : ICanConnect, IHaveConfig
{
private IConnectionConfig _config;
public BaseConnection(IConnectionConfig config)
{
_config = config;
}
public IConnectionConfig GetConfig() => _config;
public abstract void Connect();
public abstract void Disconnect();
}因此,您可以添加TCP和So 232连接的实现:
public class TcpConnection: BaseConnection
{
public TcpConnection(ITcpConfiguration tcpConfig) : base(tcpConfig) { }
public override void Connect()
{
ITcpConfiguration config = (ITcpConfiguration) base.GetConfig();
// here you have the IP-Adress
}
public override void Disconnect()
{
throw new NotImplementedException();
}
}
public class IRS232Connection : BaseConnection
{
public IRS232Connection(IRS232Configuration rs232Config) : base(rs232Config) { }
public override void Connect()
{
IRS232Configuration config = (IRS232Configuration) base.GetConfig();
// here you have the Baud-Rate
}
public override void Disconnect()
{
throw new NotImplementedException();
}
}使用List<BaseConnection>,您可以使用连接/断开方法(多态):
// initialization
List<BaseConnection> connections = new List<BaseConnection>()
{
new TcpConnection(new TcpConfiguration("192.158.1.38")),
new RS232Connection(new RS232Configuration(2400)),
};
// use them
foreach(BaseConnection con in connections)
{
con.Connect();
con.Disconnect();
}如果以后要更改配置,例如TcpConnections的IP入口:
foreach (TcpConnection con in connections.OfType<TcpConnection>())
{
ITcpConfiguration config = con.GetConfig() as ITcpConfiguration;
config.IpAdress = "192.158.1.39";
}发布于 2022-10-19 10:50:47
如果使用依赖项注入,则可以在启动中配置每个连接,如果配置是动态的,则可以配置来自工厂的连接。
interface IConnection
{
void Connect();
void Disconnect();
}
class RS232Conifguration
{
public int BaudRate { get; set; }
}
class RS232 : IConnection
{
private readonly RS232Conifguration _conifguration;
public RS232(RS232Conifguration conifguration)
{
_conifguration = conifguration;
}
public void Connect()
{
throw new System.NotImplementedException();
}
public void Disconnect()
{
throw new System.NotImplementedException();
}
}
public class TCPConfiguration
{
public int IpAddress { get; set; }
}
public class TCP :IConnection
{
private readonly TCPConfiguration _configuration;
public TCP(TCPConfiguration configuration)
{
_configuration = configuration;
}
public void Connect()
{
throw new System.NotImplementedException();
}
public void Disconnect()
{
throw new System.NotImplementedException();
}
}
var services = new ServiceCollection();
services.AddSingleton(new TCPConfiguration() { IpAddress = 423 });
services.AddScoped<IConnection, TCP>();
services.AddSingleton(new RS232Conifguration() { BaudRate = 4 });
services.AddScoped<IConnection, RS232>();
var provider = services.BuildServiceProvider();
var connections = provider.GetServices<IConnection>();https://stackoverflow.com/questions/74123322
复制相似问题