You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: feature-toggle/README.md
+64-26Lines changed: 64 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -16,13 +16,13 @@ tag:
16
16
17
17
## Intent
18
18
19
-
A technique used in software development to control and manage the rollout of specific features or functionality in a program without changing the code. It can act as an on/off switch for features depending on the status or properties of other values in the program. This is similar to A/B testing, where features are rolled out based on properties such as location or device. Implementing this design pattern can increase code complexity, and it is important to remember to remove redundant code if this design pattern is being used to phase out a system or feature.
19
+
To enable or disable features in a software application dynamically without deploying new code.
20
20
21
21
## Explanation
22
22
23
23
Real-world Example
24
24
25
-
> This design pattern works really well in any sort of development, in particular mobile development. Say you want to introduce a feature such as dark mode, but you want to ensure that the feature works correctly and don't want to roll out the feature to everyone immediately. You write in the code, and have it switched off as default. From here, it is easy to turn on the code for specific users based on selection criteria, or randomly. This will also allow the feature to be turned off easily without any drastic changes to the code, or any need for redeployment or updates.
25
+
> A real-world example of the Feature Toggle pattern is Netflix's rollout of new user interface features. When Netflix decides to introduce a new feature, such as a redesigned homepage layout or a new recommendation algorithm, they use feature toggles to control the release. Initially, the new feature is toggled off for most users, allowing only a small group of users (e.g., beta testers) to experience and provide feedback on the feature. Based on the feedback and performance metrics, Netflix can quickly toggle the feature on for a broader audience or turn it off if issues are detected, all without redeploying the application. This approach allows Netflix to continuously innovate and improve their platform while minimizing risk and ensuring a stable user experience.
26
26
27
27
In plain words
28
28
@@ -32,36 +32,74 @@ Wikipedia says
32
32
33
33
> A feature toggle in software development provides an alternative to maintaining multiple feature branches in source code. A condition within the code enables or disables a feature during runtime. In agile settings the toggle is used in production, to switch on the feature on demand, for some or all the users.
34
34
35
-
## Programmatic Example
35
+
**Programmatic Example**
36
36
37
-
This example shows Java code that allows a feature to show when it is enabled by the developer, and when a user is a Premium member of the application. This is useful for subscription locked features.
37
+
This Java code example demonstrates how to display a feature when it is enabled by the developer and the user is a Premium member of the application. This approach is useful for managing subscription-locked features.
38
+
39
+
The Feature Toggle pattern enables the seamless activation or deactivation of entire code executions. This allows features to be managed dynamically based on user information or configuration properties.
40
+
41
+
Key Components:
42
+
43
+
1.`PropertiesFeatureToggleVersion`: This class uses properties to control the feature toggle. The properties determine whether the enhanced version of the welcome message, which is personalized, is turned on or off.
44
+
45
+
2.`TieredFeatureToggleVersion`: This class uses user information to control the feature toggle. The feature of the personalized welcome message is dependent on the user group the user is in.
46
+
47
+
3.`User`: This class represents the user of the application.
48
+
49
+
4.`UserGroup`: This class represents the group the user belongs to.
38
50
39
51
```java
40
-
publicclassFeatureToggleExample {
41
-
// Bool for feature enabled or disabled
42
-
privatestaticboolean isNewFeatureEnabled =false;
43
-
44
-
publicstaticvoidmain(String[] args) {
45
-
boolean userIsPremium =true; // Example: Check if the user is a premium user
46
-
47
-
// Check if the new feature should be enabled for the user
48
-
if (userIsPremium && isNewFeatureEnabled) {
49
-
// User is premium and the new feature is enabled
50
-
showNewFeature();
51
-
}
52
-
}
53
-
54
-
privatestaticvoidshowNewFeature() {
55
-
// If user is allowed to see locked feature, this is where the code would go
56
-
}
52
+
publicstaticvoid main(String[] args) {
53
+
54
+
// Demonstrates the PropertiesFeatureToggleVersion running with properties
55
+
// that set the feature toggle to enabled.
56
+
57
+
finalvar properties =newProperties();
58
+
properties.put("enhancedWelcome", true);
59
+
var service =newPropertiesFeatureToggleVersion(properties);
60
+
finalvar welcomeMessage = service.getWelcomeMessage(newUser("Jamie No Code"));
61
+
LOGGER.info(welcomeMessage);
62
+
63
+
// Demonstrates the PropertiesFeatureToggleVersion running with properties
64
+
// that set the feature toggle to disabled. Note the difference in the printed welcome message
65
+
// where the username is not included.
66
+
67
+
finalvar turnedOff =newProperties();
68
+
turnedOff.put("enhancedWelcome", false);
69
+
var turnedOffService =newPropertiesFeatureToggleVersion(turnedOff);
70
+
finalvar welcomeMessageturnedOff =
71
+
turnedOffService.getWelcomeMessage(newUser("Jamie No Code"));
72
+
LOGGER.info(welcomeMessageturnedOff);
73
+
74
+
// Demonstrates the TieredFeatureToggleVersion setup with
75
+
// two users: one on the free tier and the other on the paid tier. When the
76
+
// Service#getWelcomeMessage(User) method is called with the paid user, the welcome
77
+
// message includes their username. In contrast, calling the same service with the free tier user results
78
+
// in a more generic welcome message without the username.
The code shows how simple it is to implement this design pattern, and the criteria can be further refined or broadened should the developers choose to do so.
61
-
62
-
## Class diagram
95
+
Running the example produces the following output.
0 commit comments