siteName > > feature-toggle
Threads
java ( 107555 ) - com.iluwatar.fe ( 108301 ) stack: com.iluwatar.featuretoggle.App.main(App.java:72) org.codehaus.mojo.exec.ExecJavaMojo.doMain(ExecJavaMojo.java:385) org.codehaus.mojo.exec.ExecJavaMojo.doExec(ExecJavaMojo.java:374) org.codehaus.mojo.exec.ExecJavaMojo.lambda$execute$0(ExecJavaMojo.java:296) java.base/java.lang.Thread.run(Thread.java:840)
/*
 * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
 *
 * The MIT License
 * Copyright © 2014-2022 Ilkka Seppälä
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.iluwatar.featuretoggle;

import com.iluwatar.featuretoggle.pattern.Service;
import com.iluwatar.featuretoggle.pattern.propertiesversion.PropertiesFeatureToggleVersion;
import com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion;
import com.iluwatar.featuretoggle.user.User;
import com.iluwatar.featuretoggle.user.UserGroup;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;

/**
 * The Feature Toggle pattern allows for complete code executions to be turned on or off with ease.
 * This allows features to be controlled by either dynamic methods just as {@link User} information
 * or by {@link Properties}. In the App below there are two examples. Firstly the {@link Properties}
 * version of the feature toggle, where the enhanced version of the welcome message which is
 * personalised is turned either on or off at instance creation. This method is not as dynamic as
 * the {@link User} driven version where the feature of the personalised welcome message is
 * dependent on the {@link UserGroup} the {@link User} is in. So if the user is a member of the
 * {@link UserGroup#isPaid(User)} then they get an enhanced version of the welcome message.
 *
 * <p>Note that this pattern can easily introduce code complexity, and if not kept in check can
 * result in redundant unmaintained code within the codebase.
 */
@Slf4j
public class App {

  /**
   * Block 1 shows the {@link PropertiesFeatureToggleVersion} being run with {@link Properties}
   * setting the feature toggle to enabled.
   *
   * <p>Block 2 shows the {@link PropertiesFeatureToggleVersion} being run with {@link Properties}
   * setting the feature toggle to disabled. Notice the difference with the printed welcome message
   * the username is not included.
   *
   * <p>Block 3 shows the {@link
   * com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion} being set up with
   * two users on who is on the free level, while the other is on the paid level. When the {@link
   * Service#getWelcomeMessage(User)} is called with the paid {@link User} note that the welcome
   * message contains their username, while the same service call with the free tier user is more
   * generic. No username is printed.
   *
   * @see User
   * @see UserGroup
   * @see Service
   * @see PropertiesFeatureToggleVersion
   * @see com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion
   */
  public static void main(String[] args) {

    final var properties = new Properties();
    properties.put("enhancedWelcome", true);
    var service = new PropertiesFeatureToggleVersion(properties);
    final var welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code"));
    LOGGER.info(welcomeMessage);

    // ---------------------------------------------

    final var turnedOff = new Properties();
    turnedOff.put("enhancedWelcome", false);
    var turnedOffService = new PropertiesFeatureToggleVersion(turnedOff);
    final var welcomeMessageturnedOff =
        turnedOffService.getWelcomeMessage(new User("Jamie No Code"));
    LOGGER.info(welcomeMessageturnedOff);

    // --------------------------------------------

    var service2 = new TieredFeatureToggleVersion();

    final var paidUser = new User("Jamie Coder");
    final var freeUser = new User("Alan Defect");

    UserGroup.addUserToPaidGroup(paidUser);
    UserGroup.addUserToFreeGroup(freeUser);

    final var welcomeMessagePaidUser = service2.getWelcomeMessage(paidUser);
    final var welcomeMessageFreeUser = service2.getWelcomeMessage(freeUser);
    LOGGER.info(welcomeMessageFreeUser);
    LOGGER.info(welcomeMessagePaidUser);
  }
}
Variables All
No.FromNameValue
1class@72LOGGERLogger[com.iluwatar.featuretoggle.App]
272args[Ljava.lang.String;@664991ea
END 0 00
Output All Filter Merge
Process FilterThread Filter
107555 java 108301 com.iluwatar.fe
No.PNPIDTIDTNTLMessage
1java107555108301com.iluwatar.fecom.iluwatar.featuretoggle.AppIWelcome Jamie No Code. You're using the enhanced welcome message.
2java107555108301com.iluwatar.fecom.iluwatar.featuretoggle.AppIWelcome to the application.
3java107555108301com.iluwatar.fecom.iluwatar.featuretoggle.AppII suppose you can use this software.
4java107555108301com.iluwatar.fecom.iluwatar.featuretoggle.AppIYou're amazing Jamie Coder. Thanks for paying for this awesome software.
END 0 0 0 0 0 00
Project:JavaDesignPatterns
Update:20240509
Commit:bf6456ba6
Source Code:feature-toggle
BuildTool:Java17
Compiler:Java17
Runtime:Openjdk17
System:MySystemD
Kernel:Linux5.10.211
Cpu:Intel:Corei7-7700K
Machine:AwesomeMachine