加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 装饰图案设计

发布时间:2020-12-15 01:05:52 所属栏目:Java 来源:网络整理
导读:我对模式很新,我正在研究装饰模式,我必须编写一个程序. 在线学习,我找到了一个Decorator模式的例子(它是Java伪代码): class Solution1{ static interface Component { void doStuff(); } static class MyComponent implements Component { public void doSt

我对模式很新,我正在研究装饰模式,我必须编写一个程序.

在线学习,我找到了一个Decorator模式的例子(它是Java伪代码):

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
    }
};

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

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

在我看来,第二个例子可以在可以使用Decorator模式的相同情况下使用,但它更灵活(例如,您可以删除或重新排序列表中的组件),所以我的问题:

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

最佳答案
解决方案2实际上是装饰器图案和复合图案之间的混合.

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

关于使用这两种模式的更一般的答案见
Difference between the Composite Pattern and Decorator Pattern?

这是关键答案:

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

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读