Modern CI/CD development practices often mean a choice between complex branching strategies (with implications for pipeline throughput) or releasing partially implemented features configured off by default.
FeatureToggle is an interface with two implementations representing two possible states of your feature (Enabled, or Disabled).
You can create a FeatureToggle implementation either through Enabled.of or Disabled.of, which creates an Enabled Feature Toggle and a Disabled Feature Toggle respectively.
Using your Feature
Feature Toggle provides a number of useful methods : map, flatMap, forEach, isEnabled and isDisabled. Methods that accept a Function (map, flatMap) or Consumer (forEach) only call the supplied Function or Consumer if the Feature is Enabled. This allows you to use the FeatureToggle itself to control access to your feature.
Via a guarded Service
If your Feature is controlled by an instance of an Object/ Service, for example, that Object can be placed inside the FeatureToggle and only accessed via map, flatMap or forEach.
In the example above, when the map method is called, Feature.doWork will only be executed, if FeatureToggle is an instance of Enable.
Via guarded data
Alternatively, we can pass data to the FeatureToggle, and when the toggle is enabled, pass that data to external methods or functions. E.g.
In this example, the doWork method will be called and provided with the embedded Data if our FeatureToggle is enabled otherwise, nothing will happen.