Skip to content

Instantly share code, notes, and snippets.

@maritaria
Created February 4, 2016 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maritaria/9ceca195a5bf2a502fee to your computer and use it in GitHub Desktop.
Save maritaria/9ceca195a5bf2a502fee to your computer and use it in GitHub Desktop.
IrcTwitch with clean code solution to the compiler check dillema. Also allows you to neatly check the feature availability in v2.0 in run time!
using System;
using System.Diagnostics;
using System.Linq;
namespace HotBot.Core
{
public class TwitchIrc
{
//Imagine this class is only available when !SOME_CONSTANT
}
internal class TwitchIrcFactory
{
private static bool _verified = false;
private static bool _featureAvailable = false;
public static bool FeatureAvailable
{
get
{
if (!_verified)
{
_verified = true;
TryEnableFeature();
}
return _featureAvailable;
}
}
[Conditional("FEATURE_TWITCH_IRC")]
private static void TryEnableFeature()
{
_featureAvailable = true;
}
//requires System.Diagnostics
//Since you cant use ! for the statement; instead have a constant that declares the feature is supported for the platform being targetted by the build
public static TwitchIrc Create(/*arguments you need to construct an instance of TwitchIrc, but figured there werent any*/)
{
if (FeatureAvailable)
{
return new TwitchIrc();
}
else
{
}
//Also i said static because you seem not to have issues with the singleton anti-pattern
return new TwitchIrc();
}
public static void Example_Before()
{
#if SOME_CONSTANT
//Every single time you need this shit in order to be compiler safe
//Not only does it break tabs
//but also if you ever do figure out how to use it on win 10 apps (getting theoretical)
//then you need to modify all the code
//Also this is for just one feature being controlled by 1 constant
//What if that starts to expand?+
var irc = new TwitchIRC();
#endif
}
public static void Example_After()
{
var irc = TwitchIrcFactory.Create();
if (irc == null)
{
return;
}
//SAFE TO USE IRC
}
}
///////////////////////////////////
/////////////////////////////////// PERSONAL FLAVOUR TO THE PATTERN USING PRIVATE CONSTRUCTOR AND NESTED CLASSES
///////////////////////////////////
public class TwitchIrc2
{
private TwitchIrc2()
{
}
public static class Factory
{
private static bool _verified = false;
private static bool _featureAvailable = false;
public static bool FeatureAvailable
{
get
{
if (!_verified)
{
_verified = true;
TryEnableFeature();
}
return _featureAvailable;
}
}
[Conditional("FEATURE_TWITCH_IRC")]
private static void TryEnableFeature()
{
_featureAvailable = true;
}
public static TwitchIrc2 CreateInstance()
{
if (FeatureAvailable)
{
return new TwitchIrc2();
}
else
{
}
return new TwitchIrc2();
}
}
public static void Example_After()
{
if (TwitchIrc2.Factory.FeatureAvailable)
{
//Grey out some UI and enable tool tip notifying the platform not supporting the feature
}
else
{
//Enable that shit
var irc = TwitchIrc2.Factory.CreateInstance();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment