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- Yes you can use factory pattern here, and you are totally fine with that. Tomorrow if you have another Ads you can easily add it to the factory.
2- There are two ways to create factory patterns. Abstract Factory pattern delegates the responsibility of object instantiation to another object via composition and Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation. It depends on your needs.
In this case, you are using Factory Method pattern. It is more easier to use than Abstact Factory pattern.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.