Created
August 4, 2021 04:08
-
-
Save wooklab/2931b272409f62bf39334291c3d1bd2d to your computer and use it in GitHub Desktop.
EnumBeanFactory for using beans of the same type.
This file contains 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 APricePolicy implements PricePolicy { | |
@OOverride | |
public void apply(PolicyInfo policyInfo) { | |
// implements logic.. | |
} | |
} |
This file contains 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
@Service | |
@RequiredArgsConstructor | |
public class Client { | |
private final EnumBeanFactory enumBeanFactory; | |
public void process(PolicyInfo policyInfo) { | |
// .. | |
enumBeanFactory.getBean(PricePolicyType.A).apply(policyInfo); | |
// or | |
enumBeanFactory.getBean(PricePolicyType.B).apply(policyInfo); | |
// or | |
enumBeanFactory.getBean(PricePolicyType.C).apply(policyInfo); | |
// .. | |
} | |
} |
This file contains 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 EnumBeanAble<T> { | |
Class<? extends T> serviceClass(); | |
} |
This file contains 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
@Service | |
@RequiredArgsConstructor | |
public class EnumBeanFactory { | |
private final ApplicationContext applicationContext; | |
public <T> T getBean(EnumBeanAble<T> e) { | |
return applicationContext.getBean(e.serviceClass()); | |
} | |
} |
This file contains 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
@RequiredArgsConstructor | |
public enum PricePolicyType implements EnumBeanAble<PricePolicy> { | |
A(APricePolicy.class), | |
B(BPricePolicy.class), | |
C(CPricePolicy.class); | |
private final Class<? extends PricePolicy> serviceClass; | |
@Override | |
public Class<? extends PricePolicy> serviceClass() { | |
return serviceClass; | |
} | |
} |
This file contains 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 PricePolicy { | |
void apply(PolicyInfo policyInfo); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment