0

I am curious about the best C# practice for the following scenario:

I have a class called Platform which contains a constructor that allows for a single argument called platformName. I would like to create an instance of an object that populates the instance with properties related to that variable name. For example...I want to create an instance of a class like so:

var spotify = new Platform("PlatformName_Spotify");

When this is instantiated, it sets properties related to Spotify (such as its brand color, its slogan, and monthly users) so that I can access them like:

spotify.color or spotify.slogan or spotify.monthlyUsers

Is it best to make dictionaries of each brand inside the Platform class and then assign the dictionary to the matching variable name that is passed? This is what I came up with, but I feel like there has to be a better way! How would you accomplish this?

public class Platform
{

    private readonly Dictionary<string, string> spotify = new Dictionary<string, string>
    {
        { "color", "green" },
        { "slogan", "Music For Everyone" },
        { "monthlyUsers", "14M" }
    };

    private readonly string color;
    private readonly string slogan;
    private readonly string monthlyUsers;

    public Platform(string platformName)
    {
        if(platformName == "PlatformName_Spotify")
        {
            this.color = spotify["color"];
            this.slogan = spotify["slogan"];
            this.monthlyUsers = spotify["monthlyUsers"];
        }
    }

}
Jordan Lewallen
  • 1,681
  • 19
  • 54
  • Or just implement the Factory pattern. – PepitoSh Jun 18 '19 at 02:08
  • 1
    Consider making `Platform` `abstract`. Then create `SpotifyPlatform` that inherits from it and sets whatever it needs to. – mjwills Jun 18 '19 at 02:08
  • @mjwills almost on track with this one, however, As there will be several platforms (unknown until runtime), I dont think I can do this because I would need to hard code an instantiation of the `SpotifyPlatform` class you mentioned in your comment, right? For a specific page, `PlatformName_Spotify` may not be passed as a constructor argument. (It may be iTunes, SoundCloud, etc) – Jordan Lewallen Jun 18 '19 at 02:22
  • `AbstractPlatform`, `SpotifyPlatform`, `ITunesPlatform`, `SoundCloudPlatform`, `GenericPlatformWhereICanPassParametersIfIWantTo` etc etc. – mjwills Jun 18 '19 at 02:31
  • Not sure how useful the dictionary is based on your comment. The example would require you to either have specialized classes for the platforms or allow passing the dictionary in. – ChiefTwoPencils Jun 18 '19 at 02:48
  • @ChiefTwoPencils looks like factory pattern is what I should be using as I don't know what classes to instantiate until runtime, hmm – Jordan Lewallen Jun 18 '19 at 02:51
  • Is the dictionary hard coded? – shingo Jun 18 '19 at 03:03
  • @shingo yes, the platforms are hardcoded as dictionaries, but the decision of which one to instantiate is not defined until runtime – Jordan Lewallen Jun 18 '19 at 03:04
  • These properties look like changeable, anyway usually I will use a config file. – shingo Jun 18 '19 at 03:09
  • [What is the basic difference between the Factory and Abstract Factory Design Patterns?](https://stackoverflow.com/a/35714637/7444103) (don't consider the accepted answer). -- [Design Patterns: Abstract Factory vs Factory Method](https://stackoverflow.com/a/4211307/7444103). – Jimi Jun 18 '19 at 05:12

0 Answers0