I am trying to improve my coding style. Consider the following scenario:
Suppose I want to define user control for an Album ASP.Net server. The goal is to allow the user to select the type of album, and all other things will be done using the control.
I thought of two approaches:
1- Define the IAlbum interface and define a class (which implements IAlbum) for each type of album. For example:
public class FlashAlbum : IAlbum {
So, if the user needs a flash album, he must explicitly create a FlashAlbum object. something like:
var myFlashAlbum = new FlashAlbum(); var myJSAlbum = new JSAlbum();
The problem is that I do not want the user to deal with several types of albums. Read below to understand what I mean.
2- Define IAlbum, define a class (which implements IAlbum) for each type of album (as above) and define a class Album that does not execute , implements IAlbum. It is used to instantiate instances in its template (factory template). EnumAlbumType type definition:
Public Enum AlbumTypes { FlashAlbum, JSAlbum }
Now define the constructor of the Album Parent class, which takes a parameter of type EnumAlbumTypes and creates the corresponding album based on the parameter. I prefer this method. But I am not very familiar with the factory pattern. I want the user to create albums like:
var myFlashAlbum = new Album(AlbumTypes.FlashAlbum); // Now set FlashAlbum custom properties. var myJSAlbum = new Album(AlbumTypes.JSAlbum); // Now set JSAlbum custom properties.
What is the best approach to achieve this?
Thank you and sorry for the long post.
Kamyar
source share