我根据数据动态选择正确实现的设计模式的名称是什么?
What is the name of the design pattern where I dynamically pick the correct implementation based on data?
最初我根据来自 Java EE 服务器的域和领域数据选择了正确的用户实现。然而那是公司代码,所以我不得不用数字重写这个例子。我希望底层模式仍然是可以理解的。对于那些不熟悉 CDI 的人,@Inject Instance 允许您遍历接口的所有实现。
public class NumberPicker {
@Inject
private Instance<NumberMapper> mappers;
public Number parse (String string) {
for( NumberMapper mapper : mappers ){
if( mapper.isApplicable(string) ){
return mapper.apply(string);
}
}
throw new InvalidArgumentException("Can not parse " + string);
}
}
public interface NumberMapper {
boolean isApplicable (String string);
Number apply (String string);
}
public class ByteMapper implements NumberMapper {
@Override
public boolean isApplicable (String string) {
return string.length() == 1;
}
@Override
public Number apply (String string) {
return (byte) string.charAt(0);
}
}
public class IntegerMapper implements NumberMapper {
@Override
public boolean isApplicable (String string) {
if( string.length() == 1 ){
return false;
}
try {
Integer.parseInt(string);
return true;
}catch( NumberFormatException e ){
return false;
}
}
@Override
public Number apply (String string) {
return Integer.parseInt(string);
}
}
public class FloatMapper implements NumberMapper
@Override
public boolean isApplicable (String string) {
if( string.length() == 1 ) {
return false;
}
try {
Integer.parseInt(string);
return false;
}catch( NumberFormatException e){
}
try {
Float.parseFloat(string);
return true;
}catch( NumberFormatException e){
return false;
}
}
@Override
public Number apply (String string) {
return Float.parseFloat(string);
}
}
我认为你在谈论 策略模式 :
public interface Strategy {
boolean test();
void execute();
}
public class FirstStrategy implements Strategy{
@Override
public boolean test() {
//returns true or false
}
@Override
public void execute() {
//your custom implementation
}
}
public class SecondStrategy implements Strategy{
@Override
public boolean test() {
//returns true or false
}
@Override
public void execute() {
//your custom implementation
}
}
那么你就有了你的策略列表:
List<Strategy> strategiesList = List.of(new FirstStrategy(), new SecondStrategy());
因此:
strategiesList.stream()
.filter(aStrategy -> aStrategy.test())
.findFirst()
.ifPresent(aStrategy -> aStrategy.execute());
最初我根据来自 Java EE 服务器的域和领域数据选择了正确的用户实现。然而那是公司代码,所以我不得不用数字重写这个例子。我希望底层模式仍然是可以理解的。对于那些不熟悉 CDI 的人,@Inject Instance 允许您遍历接口的所有实现。
public class NumberPicker {
@Inject
private Instance<NumberMapper> mappers;
public Number parse (String string) {
for( NumberMapper mapper : mappers ){
if( mapper.isApplicable(string) ){
return mapper.apply(string);
}
}
throw new InvalidArgumentException("Can not parse " + string);
}
}
public interface NumberMapper {
boolean isApplicable (String string);
Number apply (String string);
}
public class ByteMapper implements NumberMapper {
@Override
public boolean isApplicable (String string) {
return string.length() == 1;
}
@Override
public Number apply (String string) {
return (byte) string.charAt(0);
}
}
public class IntegerMapper implements NumberMapper {
@Override
public boolean isApplicable (String string) {
if( string.length() == 1 ){
return false;
}
try {
Integer.parseInt(string);
return true;
}catch( NumberFormatException e ){
return false;
}
}
@Override
public Number apply (String string) {
return Integer.parseInt(string);
}
}
public class FloatMapper implements NumberMapper
@Override
public boolean isApplicable (String string) {
if( string.length() == 1 ) {
return false;
}
try {
Integer.parseInt(string);
return false;
}catch( NumberFormatException e){
}
try {
Float.parseFloat(string);
return true;
}catch( NumberFormatException e){
return false;
}
}
@Override
public Number apply (String string) {
return Float.parseFloat(string);
}
}
我认为你在谈论 策略模式 :
public interface Strategy {
boolean test();
void execute();
}
public class FirstStrategy implements Strategy{
@Override
public boolean test() {
//returns true or false
}
@Override
public void execute() {
//your custom implementation
}
}
public class SecondStrategy implements Strategy{
@Override
public boolean test() {
//returns true or false
}
@Override
public void execute() {
//your custom implementation
}
}
那么你就有了你的策略列表:
List<Strategy> strategiesList = List.of(new FirstStrategy(), new SecondStrategy());
因此:
strategiesList.stream()
.filter(aStrategy -> aStrategy.test())
.findFirst()
.ifPresent(aStrategy -> aStrategy.execute());