Togglz is an implementation of the Feature Toggles pattern for Java. Feature Toggles are very useful especially in the context of agile development practices and are used by big sites like Flickr.
Martin Fowler defines Feature Toggles like this:
The basic idea is to have a configuration file that defines a bunch of toggles for various features you have pending. The running application then uses these toggles in order to decide whether or not to show the new feature.
So the basic idea is to define a toggle for each new feature you implement. As this toggle is disabled by default you can safely deploy the application to the production servers even if the feature is not completely finished and tested. If the feature implementation is complete you can enable it in the production system at any point in time.
This concepts brings many advantages:
Togglz has been developed for providing simple to use feature flags for Java applications.
The following table contains definitions for some basic terms. It is very important to understand these because they will be used in the following sections.
Term | Description |
---|---|
feature | A feature represents a single characteristic that you can enable or disable. Features in Togglz are typically represented by a Java enum with one value for each feature. |
enabled / disabled | A feature is either enabled or disabled for the entire application. Note that being enabled does not guarantee that the feature will be active, unless no activation strategy has been defined for it. |
active / inactive | Enabled features that are also active are visible and usable by the users while enabled, but inactive, features are hidden and therefore not usable. The section below, titled "The Feature State" explains this in more detail. |
Features are simply declared using a standard Java enum type. Annotations can be used to enrich the feature with additional metadata.
public enum MyFeatures implements Feature { @Label("First Feature") FEATURE_ONE, @Label("Second Feature") @ActiveByDefault FEATURE_TWO; public boolean isActive() { return FeatureContext.getFeatureManager().isActive(this); } }
The Togglz API has been developed in a way that makes it very easy to check whether a feature is active
or not. Just call the isActive()
method on the corresponding feature enum value.
public void someBusinessMethod() { if( MyFeatures.FEATURE_ONE.isActive() ) { // do new exciting stuff here } }
The state of a feature in Togglz consists of the following properties:
The values of these properties can be used to determine whether a feature is active or not: