簡體   English   中英

多個代理服務器的正確設計

[英]Proper design for multiple agents server

(使用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的方法填充其數據結構。 我們不確定以上內容是否是“好的做法”設計。

如果您對我們的設計還有其他想法或見解,請告訴我們。

更新: Server也有一個GUI對象,它向其授予了一些信息的權限。 因為代理是實際生成數據(從Web上獲取數據,或從硬件傳感器獲取數據)的代理,所以它們必須直接訪問ServerManagementGUI方法。 現在,由於每個代理使用ServerServerManagementGUI不同方法和屬性,我們認為將引用傳遞給整個對象將是最方便的。

我不會將Server參考直接傳遞給代理。 代理所需的不是實際的服務器引用,而是服務器的抽象,例如IServerContext。 讓服務器對象為您實現此接口,然后在創建代理期間,您可以向代理注入IServerContext的引用。 這有助於您在服務器和代理之間建立低耦合的通信協議。 因此,您的代理不依賴於服務器實現。 另外,它還使您能夠簡單地對IServerContext進行單元測試,並確保它提供了不同代理可能需要的必需數據和行為。 另一個好處是您還可以使用任何IoC庫將上下文的多個不同實現注入到不同的代理中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM