Skip to content

Commit 3035f40

Browse files
committed
docs: update feature toggle
1 parent cbf1c77 commit 3035f40

File tree

2 files changed

+75
-28
lines changed

2 files changed

+75
-28
lines changed

feature-toggle/README.md

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ tag:
1616

1717
## Intent
1818

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.
2020

2121
## Explanation
2222

2323
Real-world Example
2424

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.
2626
2727
In plain words
2828

@@ -32,36 +32,74 @@ Wikipedia says
3232

3333
> 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.
3434
35-
## Programmatic Example
35+
**Programmatic Example**
3636

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.
3850

3951
```java
40-
public class FeatureToggleExample {
41-
// Bool for feature enabled or disabled
42-
private static boolean isNewFeatureEnabled = false;
43-
44-
public static void main(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-
private static void showNewFeature() {
55-
// If user is allowed to see locked feature, this is where the code would go
56-
}
52+
public static void main(String[] args) {
53+
54+
// Demonstrates the PropertiesFeatureToggleVersion running with properties
55+
// that set the feature toggle to enabled.
56+
57+
final var properties = new Properties();
58+
properties.put("enhancedWelcome", true);
59+
var service = new PropertiesFeatureToggleVersion(properties);
60+
final var welcomeMessage = service.getWelcomeMessage(new User("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+
final var turnedOff = new Properties();
68+
turnedOff.put("enhancedWelcome", false);
69+
var turnedOffService = new PropertiesFeatureToggleVersion(turnedOff);
70+
final var welcomeMessageturnedOff =
71+
turnedOffService.getWelcomeMessage(new User("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.
79+
80+
var service2 = new TieredFeatureToggleVersion();
81+
82+
final var paidUser = new User("Jamie Coder");
83+
final var freeUser = new User("Alan Defect");
84+
85+
UserGroup.addUserToPaidGroup(paidUser);
86+
UserGroup.addUserToFreeGroup(freeUser);
87+
88+
final var welcomeMessagePaidUser = service2.getWelcomeMessage(paidUser);
89+
final var welcomeMessageFreeUser = service2.getWelcomeMessage(freeUser);
90+
LOGGER.info(welcomeMessageFreeUser);
91+
LOGGER.info(welcomeMessagePaidUser);
5792
}
5893
```
5994

60-
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.
6396

64-
![Feature Toggle](./etc/feature-toggle.png "Feature Toggle")
97+
```
98+
07:31:50.802 [main] INFO com.iluwatar.featuretoggle.App -- Welcome Jamie No Code. You're using the enhanced welcome message.
99+
07:31:50.804 [main] INFO com.iluwatar.featuretoggle.App -- Welcome to the application.
100+
07:31:50.804 [main] INFO com.iluwatar.featuretoggle.App -- I suppose you can use this software.
101+
07:31:50.804 [main] INFO com.iluwatar.featuretoggle.App -- You're amazing Jamie Coder. Thanks for paying for this awesome software.
102+
```
65103

66104
## Applicability
67105

@@ -102,6 +140,6 @@ Trade-offs:
102140

103141
## Credits
104142

105-
* [Feature Toggle - Martin Fowler](http://martinfowler.com/bliki/FeatureToggle.html)
106143
* [Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation](https://amzn.to/4488ESM)
107144
* [Release It! Design and Deploy Production-Ready Software](https://amzn.to/3UoeJY4)
145+
* [Feature Toggle (Martin Fowler)](http://martinfowler.com/bliki/FeatureToggle.html)

feature-toggle/src/main/java/com/iluwatar/featuretoggle/App.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,18 @@ public class App {
7171
*/
7272
public static void main(String[] args) {
7373

74+
// Demonstrates the PropertiesFeatureToggleVersion running with properties
75+
// that set the feature toggle to enabled.
76+
7477
final var properties = new Properties();
7578
properties.put("enhancedWelcome", true);
7679
var service = new PropertiesFeatureToggleVersion(properties);
7780
final var welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code"));
7881
LOGGER.info(welcomeMessage);
7982

80-
// ---------------------------------------------
83+
// Demonstrates the PropertiesFeatureToggleVersion running with properties
84+
// that set the feature toggle to disabled. Note the difference in the printed welcome message
85+
// where the username is not included.
8186

8287
final var turnedOff = new Properties();
8388
turnedOff.put("enhancedWelcome", false);
@@ -86,7 +91,11 @@ public static void main(String[] args) {
8691
turnedOffService.getWelcomeMessage(new User("Jamie No Code"));
8792
LOGGER.info(welcomeMessageturnedOff);
8893

89-
// --------------------------------------------
94+
// Demonstrates the TieredFeatureToggleVersion setup with
95+
// two users: one on the free tier and the other on the paid tier. When the
96+
// Service#getWelcomeMessage(User) method is called with the paid user, the welcome
97+
// message includes their username. In contrast, calling the same service with the free tier user results
98+
// in a more generic welcome message without the username.
9099

91100
var service2 = new TieredFeatureToggleVersion();
92101

0 commit comments

Comments
 (0)