So I’m pretty amateur at program design. I’ve done a few courses at uni but they all focus on learning the syntax and very basic design principles.
Anyway, I’m writing a client in C# .Net that interacts with rtorrent via RPC. I’m storing the torrent client information in an ITorrentClient interface, and torrent information in a Torrent object. To get data about the torrent, it needs to call a function from ITorrentClient, for example:
class Torrent
{
string _hash;
ITorrentClient _client;
public Torrent(ITorrentClient client)
{
this._client = client;
}
public double UploadSpeed
{
get
{
return _client.getTorrentUploadSpeed(_hash);
}
}
}
Unfortunately this means that if you have a large amount of torrents in your torrent client (like I do), you’re going to have hundreds of wasted ITorrentClients in memory. How can I design my program so that I don’t have hundreds of useless objects floating around?
Thanks!
If your implementation of
ITorrentClientis a reference type (a class) and not a value type (a struct), the_clientproperty of eachTorrentwill hold a reference toITorrentClient, not the actual object.Consider:
There’s only one instance of
MyTorrentClientin memory, andt1andt2hold a reference to it.