Skip to content

Commit 6a97047

Browse files
committed
docs: update feature toggle
1 parent 2b58233 commit 6a97047

File tree

5 files changed

+57
-42
lines changed

5 files changed

+57
-42
lines changed

feature-toggle/README.md

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,39 @@ title: Feature Toggle
33
category: Behavioral
44
language: en
55
tag:
6-
- Extensibility
6+
- Decoupling
7+
- Extensibility
8+
- Feature management
9+
- Scalability
710
---
811

912
## Also known as
10-
Feature Flag
13+
14+
* Feature Flag
15+
* Feature Switch
1116

1217
## Intent
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+
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.
1820

1921
## Explanation
22+
2023
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.
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.
2626
2727
In plain words
28+
2829
> Feature Toggle is a way to introduce new features gradually instead of deployment all at once.
2930
3031
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.
32+
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.
3434
3535
## 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.
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.
38+
3839
```java
3940
public class FeatureToggleExample {
4041
// Bool for feature enabled or disabled
@@ -55,32 +56,52 @@ public class FeatureToggleExample {
5556
}
5657
}
5758
```
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.
59+
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.
6061

6162
## Class diagram
62-
![alt text](./etc/feature-toggle.png "Feature Toggle")
63+
64+
![Feature Toggle](./etc/feature-toggle.png "Feature Toggle")
6365

6466
## Applicability
67+
6568
Use the Feature Toggle pattern when
6669

67-
* Giving different features to different users.
70+
* Conditional feature access to different users and groups.
6871
* Rolling out a new feature incrementally.
6972
* Switching between development and production environments.
7073
* Quickly disable problematic features
7174
* External management of feature deployment
7275
* Ability to maintain multiple version releases of a feature
7376
* 'Hidden' deployment, releasing a feature in code for designated testing but not publicly making it available
7477

78+
## Known Uses
79+
80+
* Web development platforms use feature toggles to gradually roll out new features to users to ensure stability.
81+
* Enterprise applications use feature toggles to enable or disable features during runtime to cater to different market needs.
82+
7583
## Consequences
76-
Consequences involved with using the Feature Toggle pattern
7784

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
85+
Benefits:
86+
87+
* Facilitates A/B testing and canary releases.
88+
* Allows for quicker rollback and minimal risk deployments.
89+
* Enables conditional feature execution without redeploying the application.
90+
91+
Trade-offs:
92+
93+
* Code complexity is increased.
94+
* Testing of multiple states is harder and more time-consuming.
95+
* Potential for technical debt if toggles remain in the code longer than necessary.
96+
* Risk of toggle misconfiguration leading to unexpected behavior.
97+
98+
## Related Patterns
99+
100+
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Both patterns allow changing the behavior of software at runtime. The Feature Toggle changes features dynamically, while the Strategy allows switching algorithms or strategies.
101+
* [Observer](https://java-design-patterns.com/patterns/observer/): Useful for implementing feature toggles by notifying components of feature state changes, which allows dynamic feature modification without restarts.
82102

83103
## Credits
84104

85-
* [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/)
105+
* [Feature Toggle - Martin Fowler](http://martinfowler.com/bliki/FeatureToggle.html)
106+
* [Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation](https://amzn.to/4488ESM)
107+
* [Release It! Design and Deploy Production-Ready Software](https://amzn.to/3UoeJY4)

feature-toggle/src/main/java/com/iluwatar/featuretoggle/pattern/propertiesversion/PropertiesFeatureToggleVersion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public PropertiesFeatureToggleVersion(final Properties properties) {
7777
/**
7878
* Generate a welcome message based on the user being passed and the status of the feature toggle.
7979
* If the enhanced version is enabled, then the message will be personalised with the name of the
80-
* passed {@link User}. However if disabled then a generic version fo the message is returned.
80+
* passed {@link User}. However, if disabled then a generic version fo the message is returned.
8181
*
8282
* @param user the {@link User} to be displayed in the message if the enhanced version is enabled
8383
* see {@link PropertiesFeatureToggleVersion#isEnhanced()}. If the enhanced version is
8484
* enabled, then the message will be personalised with the name of the passed {@link
85-
* User}. However if disabled then a generic version fo the message is returned.
85+
* User}. However, if disabled then a generic version fo the message is returned.
8686
* @return Resulting welcome message.
8787
* @see User
8888
*/

feature-toggle/src/main/java/com/iluwatar/featuretoggle/pattern/tieredversion/TieredFeatureToggleVersion.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030

3131
/**
3232
* This example of the Feature Toggle pattern shows how it could be implemented based on a {@link
33-
* User}. Therefore showing its use within a tiered application where the paying users get access to
33+
* User}. Therefore, showing its use within a tiered application where the paying users get access to
3434
* different content or better versions of features. So in this instance a {@link User} is passed in
3535
* and if they are found to be on the {@link UserGroup#isPaid(User)} they are welcomed with a
36-
* personalised message. While the other is more generic. However this pattern is limited to simple
36+
* personalised message. While the other is more generic. However, this pattern is limited to simple
3737
* examples such as the one below.
3838
*
3939
* @see Service
@@ -66,7 +66,7 @@ public String getWelcomeMessage(User user) {
6666

6767
/**
6868
* Method that checks if the welcome message to be returned is the enhanced version. For this
69-
* instance as the logic is driven by the user group. This method is a little redundant. However
69+
* instance as the logic is driven by the user group. This method is a little redundant. However,
7070
* can be used to show that there is an enhanced version available.
7171
*
7272
* @return Boolean value {@code true} if enhanced.

feature-toggle/src/test/java/com/iluwatar/featuretoggle/pattern/propertiesversion/PropertiesFeatureToggleVersionTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ class PropertiesFeatureToggleVersionTest {
4040

4141
@Test
4242
void testNullPropertiesPassed() {
43-
assertThrows(IllegalArgumentException.class, () -> {
44-
new PropertiesFeatureToggleVersion(null);
45-
});
43+
assertThrows(IllegalArgumentException.class, () -> new PropertiesFeatureToggleVersion(null));
4644
}
4745

4846
@Test

feature-toggle/src/test/java/com/iluwatar/featuretoggle/user/UserGroupTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,13 @@ void testAddUserToPaidGroup() {
5353
void testAddUserToPaidWhenOnFree() {
5454
var user = new User("Paid User");
5555
UserGroup.addUserToFreeGroup(user);
56-
assertThrows(IllegalArgumentException.class, () -> {
57-
UserGroup.addUserToPaidGroup(user);
58-
});
56+
assertThrows(IllegalArgumentException.class, () -> UserGroup.addUserToPaidGroup(user));
5957
}
6058

6159
@Test
6260
void testAddUserToFreeWhenOnPaid() {
6361
var user = new User("Free User");
6462
UserGroup.addUserToPaidGroup(user);
65-
assertThrows(IllegalArgumentException.class, () -> {
66-
UserGroup.addUserToFreeGroup(user);
67-
});
63+
assertThrows(IllegalArgumentException.class, () -> UserGroup.addUserToFreeGroup(user));
6864
}
6965
}

0 commit comments

Comments
 (0)