一旦使用Lambda,此代码是否仍然是装饰器模式?

时间:2019-04-21 16:52:31

标签: java lambda decorator

使用Lambda后,下面的代码是否仍然是Decorator Pattern?似乎不再需要'Concrete'类了。

我正在尝试学习如何将Lambda应用于Pluralsight Design Pattern类(用Java 7 vs Java 8编写)中的每个示例。

我想知道我编写的此方法是否是功能正常的Java程序员会使用的方法?

    /**
     * In this, you only need one interface.  Is it still a decorator pattern?
     * uses Lambda and forEach
     */
    public static void main_three() {

        System.out.println("\n\n***All Lambda-ed Together:");

        SandwichComponent sandwichComponent = () -> "Bread";

        SandwichComponent vegiDecorator = () -> sandwichComponent.make() + " + Vegi";

        SandwichComponent dressingDecorator = () -> vegiDecorator.make() + " + Dressing";

        SandwichComponent meatDecorator = () -> dressingDecorator.make() + " + Meat";

        SandwichComponent pickleDecorator = () -> meatDecorator.make() + " + pickle";

        SandwichComponent fancyDecorator = () -> pickleDecorator.make() + " + a touch of fancy";

        List<SandwichComponent> SandwichList = new ArrayList<SandwichComponent>() {
            {
                add(sandwichComponent);
                add(vegiDecorator);
                add(dressingDecorator);
                add(meatDecorator);
                add(pickleDecorator);
                add(fancyDecorator);
            }
        };

        SandwichList.forEach(b -> System.out.println(b.make()));
    }

0 个答案:

没有答案