I have a collection of custom objects which are mirrored in the UI
Interface (extremely simplified)
public interface IMyInterface
{
string Name { get; set; }
int Data { get; set; }
}
Collection
MyCollection<IMyInterface> Foo;
UI (which displays contents of Foo in a sensible way, e.g.)
Name: [ asdf ] Data: [ 1234 ]
Name: [ qwer ] Data: [ 5678 ]
Name: [ zxcv ] Data: [ 0000 ]
I now have the need in my program to be able to open two IMyInterface types and display the average data, along with single IMyInterface objects:
Name: [ asdf ] Data: [ 1234 ]
Name: [ qwer ] Data: [ 5678 ]
Name: [ zxcv ] Data: [ 0000 ]
Name: [ 2 Objects (asdf, qwer) ] Data: [ 3456 ]
I must be able to get out asdf and qwer from this new average.
What is the best way I can achieve this? I feel I have three options:
- Make a type which inherits from
IMyInterfacewith an internal
collection ofIMyInterfaces. However, this feels a bit dirty as
some of the properties are not averageable. - Replace
Foowith `MyCollection> and where ever I use it, make a check to the count of the inner collection - Turn MyCollection into a dictionary-like object where I can specify a “group” for each object then I can get out objects by their group ID.
Any suggestions welcome.
You can use the Composite design pattern, a good sample of which is the controls tree in .net:
Each IMyInterace can have a collection of IMyInterfaces, and its your choice what to do with the irrelevant properties (you can always return a null)