Best Design Template for the Next Scenario

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 { // Implement IAlbum Methods... // FlashAlbum-Specific Properties/Methods. } public class JSAlbum : IAlbum { // Implement IAlbum Methods... // JSAlbum-Specific Properties/Methods. } 

So, if the user needs a flash album, he must explicitly create a FlashAlbum object. something like:

 var myFlashAlbum = new FlashAlbum(/*FlashAlbumParameters*/); var myJSAlbum = new JSAlbum(/*JSAlbumParameters*/); 

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.

+7
inheritance oop design-patterns interface
source share
2 answers

The Factory template is a good option if you want to encapsulate a decision about a specific type of object to create. The create method should return the type of the album, so it will look like this:

  var myFlashAlbum = Album.create(AlbumTypes.FlashAlbum); var myJSAlbum = Album.create(AlbumTypes.JSALbum); 

If the process of creating albums is significantly different, you can consider the builder pattern. This allows you to encapsulate complex creation logic.

+4
source share

If you don’t want the user to care about which album they have, then why do you even give them a choice?

Inheritance seems to be the right choice. I do not see any evidence or arguments in favor of another option.

0
source share

All Articles