Skip to content

Instantly share code, notes, and snippets.

@bruinbrown
Last active December 21, 2015 05:28
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 bruinbrown/6256618 to your computer and use it in GitHub Desktop.
Save bruinbrown/6256618 to your computer and use it in GitHub Desktop.
Using predicates to reduce code coupling
public class Switch
{
private bool _isOn;
private Predicate<bool> _onCriteria;
public Switch(Predicate<bool> onPredicate)
{
_onCriteria = onPredicate;
_isOn = _onCriteria.Invoke(true); //When you see below the x will be true as that is the value we invoked it with
//We can just as easily store a control value which we pass in
//Or even make a generic switch to compare to other types
}
public void Update()
{
_isOn = _onCriteria.Invoke(true);
}
}
public static Program
{
public static void Main(string[] args)
{
var switch = new Switch(x => Settings.IsVolumeOn);
switch.Update();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment