(在.net环境下使用C#)
我们正在编写一个适当的服务器,它管理多个数据提供代理,这里有一些伪代码,因此简化了解释:
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的方法填充其数据结构。我们不确定上面的设计是否是“良好实践”设计。
如果您对我们的设计有其他想法或见解,请告诉我们。
更新: Server也有一个GUI对象,它对该对象拥有一些信息。因为代理是那些实际生成数据的人(从web获取数据,或者从硬件传感器获取数据),所以它必须能够直接访问ServerManagementGUI的方法。现在,由于每个代理都使用了Server和ServerManagementGUI的不同方法和属性,所以我们认为最方便的方法是将一个引用传递给整个对象。
发布于 2013-09-18 23:15:46
我不会直接将服务器引用传递给代理。代理需要的不是实际的服务器引用,而是服务器的抽象,比如IServerContext。让服务器对象为您实现这个接口,然后在创建代理期间,您可以将IServerContext引用注入到代理。这有助于您在服务器和代理之间建立一个低耦合的通信契约。因此您的代理不依赖于服务器实现。此外,它还使您能够简单地对IServerContext进行单元测试,并确保它提供了不同代理可能需要的所需数据和行为。其他好处是,您还可以使用任何IoC库向不同的代理注入多个不同的上下文实现。
https://stackoverflow.com/questions/18216773
复制相似问题