Java代替if和switch的方法(记录一下)

package xcc.mapTest;

/**
 * @Decription: 接口
 * @Author:
 * @Date:
 * @Email:
 **/
public interface Function {

    /**
     * 要做的事情
     */
    void invoke();
}
package xcc.mapTest;

import java.util.Map;

/**
 * @Decription: 代替'if else' 和 'switch'的方法
 * @Author:
 * @Date:
 * @Email:
 **/
public class IfFunction<K> {

    private Map<K, Function> map;

    /**
     *  通过map类型来保存对应的条件key和方法
     *
     * @param map a map
     */
    public IfFunction(Map<K, Function> map) {
        this.map = map;
    }

    /**
     * 添加条件
     *
     * @param key      需要验证的条件(key)
     * @param function 要执行的方法
     * @return this.
     */
    public IfFunction<K> add(K key, Function function) {
        this.map.put(key, function);
        return this;
    }

    /**
     * 确定key是否存在,如果存在,则执行相应的方法。
     *
     * @param key the key need to verify
     */
    public void doIf(K key) {
        if (this.map.containsKey(key)) {
            map.get(key).invoke();
        }
    }

    /**
     * 确定key是否存在,如果存在,则执行相应的方法。
     * 否则将执行默认方法
     *
     * @param key             需要验证的条件(key)
     * @param defaultFunction 要执行的方法
     */
    public void doIfWithDefault(K key, Function defaultFunction) {
        if (this.map.containsKey(key)) {
            map.get(key).invoke();
        } else {
            defaultFunction.invoke();
        }
    }

}
package xcc.mapTest;

import java.util.HashMap;

public class Test3 {
    public static void main(String[] args) {
        IfFunction<String> ifFunction = new IfFunction<>(new HashMap<>(5));

        //定义好要判断的条件和对应执行的方法
        ifFunction.add("1", () -> System.out.println("苹果"))
                .add("2", () -> System.out.println("西瓜"))
                .add("3", () -> System.out.println("橙子"));

        //要进入的条件
        ifFunction.doIf("2");
    }
}

猜你喜欢

转载自www.cnblogs.com/cat520/p/11688523.html