3
\$\begingroup\$

I'm learning design patterns and I thought that it would be a good idea to make my examples of each pattern. This is my example of the Strategy Pattern. Can you tell me is it good and what to improve?

This is the Gallery class

    public class PhotoGallery
{
    private ICollection<string> photosNames;
    private IShareStrategy shareStrategy;


    public PhotoGallery()
    {
        this.photosNames = new List<string>();
    }


    public void AddPhoto(string photoName)
    {
        this.photosNames.Add(photoName);
    }

    private void SetShareStrategy(ShareStrategy strategy)
    {
        switch (strategy)
        {
            case ShareStrategy.onFacebook:
                this.shareStrategy = new ShareOnFacebook();
                break;
            case ShareStrategy.onInstagram:
                this.shareStrategy = new ShareOnInstagram();
                break;
            case ShareStrategy.onTwitter:
                this.shareStrategy = new ShareOnTwitter();
                break;
        }
    }

    public void SharePhoto(string photoName, ShareStrategy strategy)
    {
        if (!this.photosNames.Contains(photoName))
        {
            Console.WriteLine("Invalid photo name.");
            return;
        }

        this.SetShareStrategy(strategy);
        shareStrategy.Share(photoName);
    }
}

This is The Share Strategy Enum

public enum ShareStrategy
{
    onFacebook,
    onInstagram,
    onTwitter
}

And the Strategies classes

public class ShareOnFacebook : IShareStrategy
{
    public void Share(string photoName)
    {
        Console.WriteLine($"You shared {photoName} on Facebook.");
    }
}

public class ShareOnInstagram : IShareStrategy
{
    public void Share(string photoName)
    {
        Console.WriteLine($"You shared {photoName} on Instagram.");
    }
}

public class ShareOnTwitter : IShareStrategy
{
    public void Share(string photoName)
    {
        Console.WriteLine($"You shared {photoName} on Twitter.");
    }
}
\$\endgroup\$
3
  • 4
    \$\begingroup\$ We cannot review just the pattern alone. You have use it with some real-world application. I see you have one so please edit your question and add a description about what it is doing. \$\endgroup\$
    – t3chb0t
    Apr 4, 2019 at 10:36
  • 1
    \$\begingroup\$ Looks more like Factory rather than Strategy \$\endgroup\$ Apr 10, 2019 at 0:15
  • 1
    \$\begingroup\$ Here's a more trustworthy example from wikipedia en.wikipedia.org/wiki/Strategy_pattern#C# \$\endgroup\$ Apr 10, 2019 at 0:20

1 Answer 1

2
\$\begingroup\$

I would use dependency injection. Something like

serviceCollection
   .AddTransient<IShareStrategy, ShareOnFacebook>()
   .AddTransient<IShareStrategy, ShareOnTwitter>()
   .AddTransient<IShareStrategyProvider, ShareStrategyProvider>();

public class ShareStrategyProvider : IShareStrategyProvider
{
   private readonly IEnumerable<IShareStrategy> _strategies;
   public ShareStrategyProvider(IEnumerable<IShareStrategy> strategies) 
   {
      _strategies = strategies;
   }

   public GetStrategy(ShareStrategy sharingStrategy);
   {
      return _strategies.Single(s => s.SharingStrategy == sharingStrategy);
   }
}

edit: Read up on open/closed principle, its related

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.