Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's the link to the github project.

I want to implement abstract factory instead of adding platform-dependent compilation flags in just one file and make it ugly, hence decoupled code. Moreover, I have to implement different AdNetworks on different platforms.

This is the interface:

public interface IAds 
{
    void ShowVideoAd();
    void PreLoadVideoAd();
    bool IsAdAvailable();

}

And all the PlatformAdController (Android, IOS, Amazon, Windows) will implement this interface:

    public class AmazonAdsController : IAds
    {
// Code Goes here
    }

Then in someFactoryClass I'll do:

public IAds GetObject()
{
    #if UNITY_Android
    if(platform == Amazon)
    return new AmazonAdsController
    else 
    return new AndroidAdsController
    #elif UNITY_IOS
    return new IOSAdsController
    #elif UNITY_WP10
    return new WindowsAdsController
}

This method will be called from AdManager a singleton MonoBehaviour. My current AdManager state is:

namespace MS.Ads
{
    [RequireComponent(typeof(KeyGenerator))]
    public class AdManager : MonoBehaviour
    {
        #region Fields & Properties
        [SerializeField]
        private KeysSource _keysSource;
        [SerializeField]
        [HideInInspector]
        private string _fileName;
        [SerializeField]
        [HideInInspector]
        private string _url;

        // Source from where to get keys
        public KeysSource Source
        {
            get { return _keysSource; }
            set { _keysSource = value; }
        }

        // Web URL from where to get AdKeys
        public string URL
        {
            get { return _url; }
            set { _url = value; }
        }

        // FileName from where to get Adkeys e.g adc.json
        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        // To be replaced by a FactoryObject.
        private FyberController _fyberController;
        #endregion

        #region Message Methods
        void Start()
        {            
            GetComponent<KeyGenerator>().GenerateKeys(Source, URL, FileName);
            _fyberController = new FyberController();
            _fyberController.RequestVideoAd();
        }
        #endregion

        #region Methods
        public void ShowAd()
        {
            if (_fyberController.IsVideoAdAvailable)
                _fyberController.ShowVideoAd();
        }

        public bool IsVideoAdAvailable()
        {
            return _fyberController.IsVideoAdAvailable ? true : false;
        }
        #endregion
    }
}

What I would like to know:

  1. Is factory pattern the right choice here?
  2. If yes, should I go with an interface or an abstract base class or a base class with virtual methods?
share|improve this question

closed as off-topic by RubberDuck, QPaysTaxes, Quill, ChrisWue, mdfst13 Feb 8 at 0:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

    
Please do not cross-post the same question on multiple sites. It's on gamedev and on SO. – ChrisWue Feb 7 at 23:15
    
Ok. I'll take it off from game dev. But why is this put on hold?? What's wrong with the question ? – SamBh Feb 8 at 7:11
    
Code Review is about reviewing existing working code. You're basically asking about Higher-level architecture and design of software systems which is off-topic. Essentially once you have implemented your solution we're happy to review the implementation. Your question would probably have been asked best over at programmers.stackexchange.com but since you've already posted it to SO and gamedev and here it's probably not very advisable to cross post it again. – ChrisWue Feb 8 at 19:59
  1. It looks a lot like some service with platform dependent implementation. You might use a dependency injection pattern or service locator pattern.

I think factory is not quite great as a solution cause you will not create different implementations in a single application run. Also that is not great if you need this Controller to be created only once.

  1. While it depends directly on your implementations and functionality of this service I will go with some interface. Further if implementations share same code then you might use an abstract base class to put that part of logic in it.
share|improve this answer
    
I think the factory is not quite great as a solution cause you will not create different implementations in a single application run. Do we need to create different implementation in factory method? Just separates different families and they create their own single product. And because there arent many pure DI frameworks In unity. – SamBh Feb 8 at 9:55

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