Giving properties to classes through the use of Interface
If you had classes like Page, News, etc. that describe content items and you wanted to implement the ability to add attachments (files or other types of items) to these classes would you create, for instance, an
public List<Attachment> Attachments { get; set; }
in each of these files or would you refactor this out into an Interface called IAttachmentContainer that only has this property
List<Attachment> Attachments { get; set; }
and then use this interface with all these classes (make classes implement the interface)?
As you noticed, since we used a generic collection (List) our interface did not need any additional methods or did it and I missed them?
I could end up with a lot of these collections (besides Attachments there might also be Widgets, etc.) and I want to keep the solution as clean as possible. So far this interface way is the best one found the only thing bothering me is that those interfaces are mostly methodless.
Essentially I would end up with
public class News : 开发者_运维百科IAttachmentContainer, IWidgetContainer, ...
Does this seem ok?
It seems OK if you are going to use a functions like...
ProcessAttachments(IAttachmentContainer MyObject)...
Then you could pass it any object that implemented the attachment interface...
ProcessAttachments(NewsObject);
精彩评论