Skip to content

Commit f9ae596

Browse files
authored
docs: Explanation for Feature Toggle iluwatar#2230 Finished (iluwatar#2622)
* Added documentation for Feature Toggle design pattern * Explanation for Feature Toggle Issue iluwatar#2230 finished, added intent, explanation, programmatic example, applicability and consequences
1 parent 9e2c8c5 commit f9ae596

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

feature-toggle/README.md

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,53 @@ tag:
1010
Feature Flag
1111

1212
## Intent
13-
Used to switch code execution paths based on properties or groupings. Allowing new features to be released, tested
14-
and rolled out. Allowing switching back to the older feature quickly if needed. It should be noted that this pattern,
15-
can easily introduce code complexity. There is also cause for concern that the old feature that the toggle is eventually
16-
going to phase out is never removed, causing redundant code smells and increased maintainability.
13+
A technique used in software development to control and manage the rollout of specific features or functionality in a
14+
program without changing the code. It can act as an on/off switch for features depending on the status or properties of
15+
other values in the program. This is similar to A/B testing, where features are rolled out based on properties such as
16+
location or device. Implementing this design pattern can increase code complexity, and it is important to remember to
17+
remove redundant code if this design pattern is being used to phase out a system or feature.
18+
19+
## Explanation
20+
Real-world Example
21+
> This design pattern works really well in any sort of development, in particular mobile development. Say you want to
22+
> introduce a feature such as dark mode, but you want to ensure that the feature works correctly and don't want to roll
23+
> out the feature to everyone immediately. You write in the code, and have it switched off as default. From here, it is
24+
> easy to turn on the code for specific users based on selection criteria, or randomly. This will also allow the feature
25+
> to be turned off easily without any drastic changes to the code, or any need for redeployment or updates.
26+
27+
In plain words
28+
> Feature Toggle is a way to introduce new features gradually instead of deployment all at once.
29+
30+
Wikipedia says
31+
> A feature toggle in software development provides an alternative to maintaining multiple feature branches in source
32+
> code. A condition within the code enables or disables a feature during runtime. In agile settings the toggle is
33+
> used in production, to switch on the feature on demand, for some or all the users.
34+
35+
## Programmatic Example
36+
This example shows Java code that allows a feature to show when it is enabled by the developer, and when a user is a
37+
Premium member of the application. This is useful for subscription locked features.
38+
```java
39+
public class FeatureToggleExample {
40+
// Bool for feature enabled or disabled
41+
private static boolean isNewFeatureEnabled = false;
42+
43+
public static void main(String[] args) {
44+
boolean userIsPremium = true; // Example: Check if the user is a premium user
45+
46+
// Check if the new feature should be enabled for the user
47+
if (userIsPremium && isNewFeatureEnabled) {
48+
// User is premium and the new feature is enabled
49+
showNewFeature();
50+
}
51+
}
52+
53+
private static void showNewFeature() {
54+
// If user is allowed to see locked feature, this is where the code would go
55+
}
56+
}
57+
```
58+
The code shows how simple it is to implement this design pattern, and the criteria can be further refined or broadened
59+
should the developers choose to do so.
1760

1861
## Class diagram
1962
![alt text](./etc/feature-toggle.png "Feature Toggle")
@@ -24,7 +67,20 @@ Use the Feature Toggle pattern when
2467
* Giving different features to different users.
2568
* Rolling out a new feature incrementally.
2669
* Switching between development and production environments.
70+
* Quickly disable problematic features
71+
* External management of feature deployment
72+
* Ability to maintain multiple version releases of a feature
73+
* 'Hidden' deployment, releasing a feature in code for designated testing but not publicly making it available
74+
75+
## Consequences
76+
Consequences involved with using the Feature Toggle pattern
77+
78+
* Code complexity is increased
79+
* Testing of multiple states is harder and more time-consuming
80+
* Confusion between friends on why features are missing
81+
* Keeping documentation up to date with all features can be difficult
2782

2883
## Credits
2984

3085
* [Martin Fowler 29 October 2010 (2010-10-29).](http://martinfowler.com/bliki/FeatureToggle.html)
86+
* [Feature Toggle - Java Design Patterns](https://java-design-patterns.com/patterns/feature-toggle/)

0 commit comments

Comments
 (0)