本文介绍了装饰图案设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对模式非常陌生,我正在研究Decorator Pattern(装饰器模式)以编写必须编写的程序.

I'm pretty new with patterns and I'm studying Decorator Pattern for a program I have to write.

在网上学习时,我发现了一个装饰器模式的示例(它是Java伪代码):

Studying online, I found an example of a Decorator Pattern (it is Java pseudo-code):

class Solution1
{
    static interface Component
    {
        void doStuff();
    }

    static class MyComponent implements Component
    {
        public void doStuff()
        {
            // ...
        }
    }

    static class ComponentDecorator implements Component  // This is the Decorator pattern.
    {
        private final Component component;

        public ComponentDecorator(Component component)
        {
            this.component = component;
        }

        public void doStuff()
        {
            this.component.doStuff();
        }
    }

    static class ComponentDecorator1 extends ComponentDecorator
    {
        public ComponentDecorator1(Component component)
        {
            super(component);
        }

        private void doExtraStuff1()
        {
            // ...
        }

        public void doStuff()
        {
            super.doStuff();
            doExtraStuff1();
        }
    }

    static class ComponentDecorator2 extends ComponentDecorator
    {
        public ComponentDecorator2(Component component)
        {
            super(component);
        }

        private void doExtraStuff2()
        {
            // ...
        }

        public void doStuff()
        {
            super.doStuff();
            doExtraStuff2();
        }
    }

    public static void main(String[] args)
    {
        MyComponent         c   = new MyComponent();
        ComponentDecorator1 cd1 = new ComponentDecorator1(c);
        ComponentDecorator2 cd2 = new ComponentDecorator2(cd1);

        cd2.doStuff(); // Executes Component.doStuff, ComponentDecorator1.doExtraStuff1, ComponentDecorator2.doExtraStuff2
    }
};

当我分析示例时,我意识到过去我做了一个非常相似的模式,但是方式却不同:

When I analyzed the example, I realized that in the past I made a very similar pattern but in different way:

import java.util.*;

class Solution2
{
    static interface Component
    {
        void doStuff();
    }

    static class MyComponent implements Component
    {
        public void doStuff()
        {
            // ...
        }
    }

    static class ComponentDecorator implements Component  // This is NOT the Decorator pattern!
    {
        private final List<Component> components = new ArrayList<Component>();

        public ComponentDecorator()
        {
        }

        public ComponentDecorator addComponent(Component component)
        {
            this.components.add(component);
            return this;
        }

        public void removeComponent(Component component) // Can Decorator do this?
        {
            // ...
        }

        public void doStuff()
        {
            for(Component c : this.components) c.doStuff();
        }
    }

    static class ComponentDecorator1 implements Component
    {
        public ComponentDecorator1()
        {
        }

        private void doExtraStuff1()
        {
            // ...
        }

        public void doStuff()
        {
            doExtraStuff1();
        }
    }

    static class ComponentDecorator2 implements Component
    {
        public ComponentDecorator2()
        {
        }

        private void doExtraStuff2()
        {
            // ...
        }

        public void doStuff()
        {
            doExtraStuff2();
        }
    }

    public static void main(String[] args)
    {
        ComponentDecorator cd  = new ComponentDecorator();
        cd.addComponent(new MyComponent());
        cd.addComponent(new ComponentDecorator1());
        cd.addComponent(new ComponentDecorator2());

        cd.doStuff(); // Executes MyComponent.doStuff, ComponentDecorator1.doExtraStuff1, ComponentDecorator2.doExtraStuff2
    }
}

我认为,第二个示例可以在可以使用装饰器模式的相同情况下使用,但是它更灵活(例如,您可以删除或重新排列列表中的组件),所以我的问题是:

In my opinion, the second example can be used in same situations where a Decorator Pattern can be used, but it's more flexible (you may, by example, remove or reorder the components in the list), so my questions:

  • 解决方案1(正确的装饰器图案)是否比解决方案2更好?为什么?
  • 是否可以在解决方案1中添加删除实例的功能?
  • 是否可以在解决方案1中添加用于对实例重新排序的功能?

推荐答案

解决方案2实际上是装饰器模式和复合模式之间的混合.

Solution 2 is actually a mix between decorator pattern and composite pattern.

如果您只是想添加行为,我认为解决方案1优于解决方案2,如果还需要将多个对象用作一个对象,则使用解决方案1 ​​+复合模式会更好.

I think that solution 1 is better than solution 2 if what you want is just to add behaviour and using solution 1 + composite pattern is better if you also need to use multiple objects as one.

有关使用这两种模式的更一般的答案,请参见复合模式与装饰器模式之间的区别?

As a more general answer about using this two patterns seeDifference between the Composite Pattern and Decorator Pattern?

这是关键答案:

复合模式允许您以允许外部代码将整个结构视为单个实体的方式构建分层结构(例如元素树).因此,叶实体的接口与复合实体的实体完全相同.因此,实质是即使您的复合结构中的所有元素都是叶节点,而另一些则是整个结构,也具有相同的接口.用户界面通常使用这种方法来实现简单的可组合性.

The composite pattern allows you to build a hierarchical structure (such as a tree of elements) in a way that allows your external code to view the entire structure as a single entity. So the interface to a leaf entity is exactly the same as the entity for a compound entity. So the essence is that all elements in your composite structure have the same interface even though some are leaf nodes and others are entire structures. User interfaces often use this approach to allow easy composability.

http://en.wikipedia.org/wiki/Composite_pattern

装饰器模式允许一个实体完全包含另一个实体,以便使用装饰器看起来与所包含的实体相同.这允许装饰器修改其封装的内容的行为和/或内容,而无需更改实体的外观.例如,您可以使用装饰器在不更改所包含元素的任何行为的情况下添加有关所包含元素用法的日志记录输出.

The decorator pattern allows an entity to completely contain another entity so that using the decorator looks identical to the contained entity. This allows the decorator to modify the behaviour and/or content of whatever it is encapsulating without changing the outward appearance of the entity. For example, you might use a decorator to add logging output on the usage of the contained element without changing any behaviour of the contained element.

http://en.wikipedia.org/wiki/Decorator_pattern

这篇关于装饰图案设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:22