Given the following JSON:
{
"data": {
"videos": [{
"url": "www.youtube.com",
"title": "YouTube"
}, {
"url": "www.vimeo.com",
"title": "Vimeo"
}]
}
}
If I wanted to de-serialise these objects into concrete instances, i.e.:
public class YouTubeVideo
{
public string Url { get; set; }
public string Title { get; set; }
public string GetYouTubeViewCount()
{
//return youtube view count
}
}
public class VimeoVideo
{
public string Url { get; set; }
public string Title { get; set; }
public string GetVimeoAudience()
{
//return audience from video
}
}
What is the best approach for determining the instance to create? Note: I'm not talking about how to deserialise here (i.e. with JSON.NET). I'm talking about a pattern to deal with the logic of determining a concrete instance.
A factory pattern could work that deals with the logic on determining the object to create based on some property/data within the JSON - but this would break the open/closed principle if I wanted to deal with new 'Video' types.
Alternatively I could use a mapper and configure it to map to the relevant concrete instance.
Is there a better way? (not looking for a particular language solution, just an approach / pattern)
Aucun commentaire:
Enregistrer un commentaire