mardi 6 août 2019

Would I have to use Factory design pattern or State Design pattern

In my application a user would enter a link for an image or a video. According to the type of media (image, video or Pin from Pinterest) the app will determine how to display a thumbnail of the entered media link, for example :

  • If link entered is a video, generate thumbnail from video then generate an HTML code snippet that will display the thumbnail.

  • If an image, reduce the size and return HTML snippet with the smaller image as a thumbnail.

  • If Pinterest link, generate embed a Pinterest HTML code.

Whatever type of link it is, it will probably have a prepThumbnail() method which will return the thumbnail image and a getThumbnailSnippet() which will return the HTML code with an image link.

To avoid using if statements and to be able to grow the types of media links, I've decided to use the

State Pattern

where I would capture the link, whatever the media type is, as a MediaLink object/context and within that object I can set the state at run time to either HasImage state/object that has the methods prepThumbnail() and getThumbnailSnippet() which are compatible with an image link and produce the thumbnail and the code snippet that I will embed.

So I just need to add a HasVideo state , HasPin state.

I was also thinking about the

Factory Method or Abstract Factory pattern

where I create an ImageLinkFactory, VideoLinkFactory, PinterestFactory and have each create the type of `MediaLink that I need.

  1. Which of these patterns is more appropriate for my use case?
  2. What source has enough examples to train me on recognizing what pattern to use for each case? something that discuss real life scenarios not just the basic TruckFactory and CarFactory.

Aucun commentaire:

Enregistrer un commentaire