0

(使用 C# 在 .net 环境中工作)

我们正在编写一个专用服务器,它管理多个数据提供代理,这里有一些伪代码,以便简化解释:

class Server
{
  ServerManagementGUI server_gui; // a GUI to display all sort of Server related data

  MonitorAgent m_agnt;
  DataAgent    d_agnt;
  // will not be allocated or init at C'tor

  public write_data1();
  public write_data2();

  public get_data5();

  // etc
}

class Agent
{
// handles generic communication and threading issues

// a reference to Server is required to write 
// the data to it's private data structures. 
// please note that a delegate to one or more function will not suffice here.

Agent(Server server); 

}

class MonitorAgent : Agent
{
 // handles task spesific issues

}

class DataAgent : Agent
{
 // handles task spesific issues
}

这个想法是代理异步收集数据,并处理任何通信和线程问题,并使用Server's 方法填充它的数据结构。我们不确定以上是否是“良好实践”设计。

如果您对我们的设计有任何其他想法或见解,请告诉我们。

更新: Server还有一个 GUI 对象,它拥有一些信息。因为代理是那些实际生成数据的人(从网络获取数据,或从硬件传感器获取数据),所以它必须能够直接访问ServerManagementGUI. 现在,由于每个代理使用不同的方法和 and 属性ServerServerManagementGUI我们认为只传递对整个对象的引用是最方便的。

4

1 回答 1

0

我不会将服务器引用直接传递给代理。代理需要的不是实际的服务器引用,而是服务器的抽象,比如 IServerContext。让服务器对象为您实现此接口,然后在创建代理期间,您可以将 IServerContext 的引用注入代理。这有助于您在服务器和代理之间建立一个低耦合的通信合同。因此,您的代理不依赖于服务器实现。此外,它还使您能够简单地对 IServerContext 进行单元测试,并确保它提供不同代理可能需要的所需数据和行为。其他好处是您还可以使用任何 IoC 库将上下文的多个不同实现注入不同的代理。

于 2013-09-18T23:15:46.207 回答