mercredi 27 septembre 2017

What design pattern is this? Adapter?

I am having a hard time defining a pattern. My colleague says it's adaptor pattern. I'm not sure. We're stuck mainly because we want to correctly name our components.

Question: Is it adapter pattern? If not what is it?

To put it in summary, it is a main component(is this the adapter?) that shares an interfaces with sub-components (are these providers?). The main components decides/orchestrates which of the sub-components are called.

Assumptions:

  1. For simplicity, we will ignore DR/IoC for now, but we understand and apply the pattern/principle.
  2. The code is not the best implemented form...feel free to suggest.
  3. My use of the words main/sub does not infer some kind of inheritence...just bad naming on my part, if confusing.
  4. It's language-agnostic, because I love contributions from C# and Java guys, and the knowledge they share.

I am using a Social Networking scenario where a main component gets stats on a hastag and instantiates the appropriate Social Sub Component ( There is a Social Component interface:

ISocialComponent
{
    SomeStatsObject GetStats(string hashTag);
}

Social Sub-Components implement ISocialComponent Interface

Twitter Sub-Component

public class TwitterSubComponent : ISocialComponent
{
    public SomeStatsObject GetStats(string hashTag)
    {
        return SomeMethodThatReturnsStatsObject(hashTag);   
    }

    private SomeMethodThatReturnsStatsObject(string hashTag)
    {
        //... Twitter-specific code goes here
    }
}

Facebook Sub-Component

public class FacebookSubComponent : ISocialComponent
{
    public SomeStatsObject GetStats(string hashTag)
    {
        return SomeMethodThatReturnsStatsObject(hashTag);
    }

    private SomeMethodThatReturnsStatsObject(string hashTag)
    {
        //... Facebook-specific code goes here
    }
}

Instagram Sub-Component

public class InstagramSubComponent : ISocialComponent
{
    public SomeStatsObject GetStats(string hashTag)
    {
        return SomeMethodThatReturnsStatsObject(hasTag);
    }

    private SomeMethodThatReturnsStatsObject(string hashTag)
    {
        //... Instagram-specific code goes here
    }
}

Main Component

There is a main social component object that calls any one of the Sub-Components (defined below) that implement the shared ISocialComponent interface

public class MainSocialComponent : ISocialComponent
{
    //this is an enum
    private RequestedNetwork _requestedNetwork{ get; set;}

    public SomeStatsObject GetStats(string hashTag) 
    {
        switch(_requestedNetwork)
        {
            case RequestedNetwork.Twitter:
                var twit = new TwitterSubComponent();
                return twit.GetStats(hashTag)
                break;

            case RequestedNetwork.Facebook:
                var fb = new FacebookSubComponent();
                return fb.GetStats(hashTag)
                break;

            case RequestedNetwork.Instagram:
                var in = new InstagramSubComponent();
                return in.GetStats(hashTag)
                break;

            default:
                throw new Exception("Undefined Social Network");
                break;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire