Created
July 2, 2011 13:01
-
-
Save eranharel/1060044 to your computer and use it in GitHub Desktop.
feature flags made easy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> | |
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> | |
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> | |
<property name="properties"> | |
<map> | |
<entry key="imaginaryFeature.implementation.bean" value="oldImaginaryFeature"/> | |
</map> | |
</property> | |
</bean> | |
<bean id="application" class="com.eranharel.SpringPolymorphicApplication"> | |
<constructor-arg ref="imaginaryFeature"/> | |
</bean> | |
<alias name="${imaginaryFeature.implementation.bean}" alias="imaginaryFeature"/> | |
<bean id="newImaginaryFeature" class="com.eranharel.NewImaginaryFeature" lazy-init="true"/> | |
<bean id="oldImaginaryFeature" class="com.eranharel.OldImaginaryFeature" lazy-init="true"/> | |
</beans> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void runApplication() { | |
// ... | |
if (useNewImplementation) { | |
executeNewImaginaryFeatureImplementation(); | |
} else { | |
executeOldImaginaryFeatureImplementation(); | |
} | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ImaginaryFeature { | |
public void executeFeature(); | |
} | |
class OldImaginaryFeature implements ImaginaryFeature { | |
@Override | |
public void executeFeature() { | |
System.out.println("old feature implementation"); | |
} | |
} | |
class NewImaginaryFeature implements ImaginaryFeature { | |
@Override | |
public void executeFeature() { | |
System.out.println("new feature implementation"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PolymorphicApplication { | |
private final ImaginaryFeature imaginaryFeature; | |
public PolymorphicApplication() { | |
this.imaginaryFeature = createImaginaryFeature(); | |
} | |
private ImaginaryFeature createImaginaryFeature() { | |
final String featureClass = System.getProperty("PolymorphicApplication.imaginaryFeature.class"); | |
try { | |
return (ImaginaryFeature) Class.forName(featureClass).newInstance(); | |
} catch (final Exception e) { | |
throw new IllegalStateException("Failed to create ImaginaryFeature of class " + featureClass, e); | |
} | |
} | |
public void runApplication() { | |
// ... | |
imaginaryFeature.executeFeature(); | |
// ... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SpringPolymorphicApplication { | |
private final ImaginaryFeature imaginaryFeature; | |
public SpringPolymorphicApplication(final ImaginaryFeature imaginaryFeature) { | |
this.imaginaryFeature = imaginaryFeature; | |
} | |
public void runApplication() { | |
// ... | |
imaginaryFeature.executeFeature(); | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment