Last active
December 21, 2015 05:28
-
-
Save bruinbrown/6256618 to your computer and use it in GitHub Desktop.
Using predicates to reduce code coupling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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