如何使用HashMap作为构建器?

时间:2016-06-26 21:24:08

标签: java hashmap command-pattern

我正在尝试运行一些方法,具体取决于到达我的字符串参数。 这个想法是从"终端"列表中激活和取消某些功能。 该类是为Command模式设计的," doProcess"将在一段时间内被召唤

我的实际解决方案很糟糕,所以我考虑制作一个HashMap。例如: map.put("邮件",activateMailsOfTerminals)

但我不知道如何动态调用函数(activateMailsOfTerminals,deactivate ...),具体取决于构造函数中的字符串列表

非常感谢!

糟糕的解决方案:

public class ActionsActivator {

    Set < Terminal > terminals = new HashSet < > ();
    Set < String > activate = new HashSet < > ();
    Set < String > deactivate = new HashSet < > ();

    public ActionsActivator(Set < Terminal > filteredTerminals, Set < String > active, Set < String > deactive) {
        this.terminals = Map().getInstance().getTerminals;
        this.activate.addAll(activate);
        this.deactivate.addAll(deactivate);
    }

    public void doProcess() {
        if (hasElement(activate, "Mail")) terminals.forEach(terminal - > terminal.activateMails());
        if (hasElement(activate, "Register")) terminals.forEach(terminal - > terminal.activateRegisters());

        if (hasElement(deactivate, "Mail")) terminals.forEach(terminal - > terminal.deactivateMails());
        if (hasElement(deactivate, "Register")) terminals.forEach(terminal - > terminal.deactivateRegisters());
    }

    private Boolean hasElement(Set < String > list, String element) {
        return list.stream().anyMatch(elem - > elem.equals(element));
    }
}

2 个答案:

答案 0 :(得分:2)

如果我理解正确,你有一组“终端”,你想要一个“激活”的对象在提供的终端上执行一些方法调用/调用。以下内容应该完成并演示:

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

public class CommandsDemo {

    public static class ActionsActivator{
        private Set<Terminal> terminals;
        private Set<Consumer<Terminal>> actionsToPerformOnTerminals = new HashSet<>();

        public ActionsActivator(Set<Terminal> terminals, Consumer<Terminal>... actionsForTerminals){
            this.terminals = terminals;
            actionsToPerformOnTerminals.addAll(Arrays.asList(actionsForTerminals));
        }

        public void doProcess(){
            for(Consumer<Terminal> action : actionsToPerformOnTerminals){
                terminals.stream().forEach(action);
            }
        }
    }

    public static class Terminal{
        private final String name;

        public Terminal(String name){
            this.name = name;
        }

        public void activateMails(){
            System.out.println("Activated mail for: "+name);
        }
        public void activateRegisters(){
            System.out.println("Activated registers for: "+name);
        }
    }

    public static void main(String[] args){
        Terminal terminalA = new Terminal("terminalA");
        Terminal terminalB = new Terminal("terminalB");
        Terminal terminalC = new Terminal("terminalC");

        Set<Terminal> terminals = new HashSet<>();
        terminals.addAll(Arrays.asList(terminalA, terminalB, terminalC));

        ActionsActivator mailActivator = new ActionsActivator(terminals, (terminal)->terminal.activateMails());

        ActionsActivator registerActivator = new ActionsActivator(terminals, (terminal)->terminal.activateRegisters());

        ActionsActivator mailAndRegisterActivator = new ActionsActivator(terminals, 
                (terminal)->terminal.activateMails(),
                (terminal)->terminal.activateRegisters());

        System.out.println("Running mail activator");
        mailActivator.doProcess();

        System.out.println("Running register activator");
        registerActivator.doProcess();

        System.out.println("Running mail & register activator");
        mailAndRegisterActivator.doProcess();
    }

}

输出:

Running mail activator
Activated mail for: terminalC
Activated mail for: terminalA
Activated mail for: terminalB
Running register activator
Activated registers for: terminalC
Activated registers for: terminalA
Activated registers for: terminalB
Running mail & register activator
Activated registers for: terminalC
Activated registers for: terminalA
Activated registers for: terminalB
Activated mail for: terminalC
Activated mail for: terminalA
Activated mail for: terminalB

使我们能够配置在每个终端上调用的命令/命令的关键是通用Consumer类,它表示使用通用对象的实例调用的能力声明类型作为参数。例如:

Consumer<String> myConsumer = (str)->{System.out.println("Hello, "+str)};

myConsumer.accept("World");
myConsumer.accept("Venus");
myConsumer.accept("Mars");

打印:

Hello, World
Hello, Venus
Hello, Mars

因此,通过传入一系列消耗Consumer<Terminal>并在所述Terminal上调用所需方法的Terminal个对象,我们可以创建一个ActionsActivator来调用提供的终端上的可配置方法/方法。

答案 1 :(得分:0)

使用地图:

private static Map<String, Consumer<Terminal>> activations = new HashMap<String, Consumer<Terminal>>() {{
    put("Mail", Terminal::activateMails);
    put("Register", Terminal::activateRegisters);
    // etc
}};
// similar for deactivate

然后使用:

public void doProcess() {
    activate.stream()
        .map(activations::get)
        .filter(a -> a != null)
        .forEach(a -> terminals.forEach(a));
    // similar for deactivate
}

这种方法以声明方式处理任意数量的动作。