简体   繁体   English

这是装饰器模式的变化还是完全是模式?

[英]Is this a variation of the decorator pattern or a pattern at all?

Below is a simplified version of code I've written to override a class method (using composition), in this case method name i'm overriding is addbuttons(); 以下是我编写的用于覆盖类方法(使用组合)的简化代码,在这种情况下,我覆盖的方法名称是addbuttons();。 The class "Screen" adds buttons to a screen whereas class "SubScreen" adds custom buttons to an instance of Screen. 类“ Screen”将按钮添加到屏幕,而类“ SubScreen”将自定义按钮添加到Screen实例。 Maybe this is not an example of decorator pattern since I'm overriding functionality instead of extending it ? 也许这不是装饰器模式的示例,因为我覆盖了功能而不是扩展了功能? Is there a better way (using a design pattern ?) to achieve same functionality ? 是否有更好的方法(使用设计模式?)来实现相同的功能?

public class Driver {

    public static void main(String args[]){
        AddComponents add1 = new Screen();
        add1.addButtons();

        Screen newScreen = new Screen();
        AddComponents add2 = new SubScreen(newScreen);
        add2.addButtons();
    }

}
    public interface AddComponents {

         public void addButtons();
        }

public class Screen implements AddComponents{

    public void addButtons() {
        //Add Buttons to screen class
    }

}
public class SubScreen implements AddComponents{

    private Screen screen;

    public SubScreen(Screen screen){
        this.screen = screen;
    }

    public void addButtons() {
        //add different custom buttons to Screen class
    }

}

Another possibility would be to call it Proxy . 另一种可能性是将其称为Proxy Decorator and Proxy are technically very similar - the difference is - in most cases - not a technical one but based on the intention. 装饰器代理在技​​术上非常相似-区别-在大多数情况下-不是技术性的,而是基于意图的。 Your example is a little bit minimal and therefore it is hard to guess the intention correctly. 您的示例有点少,因此很难正确猜出意图。

Edit 编辑

At the detailed level: Screen and SubScreen do not share any code. 在详细级别上: ScreenSubScreen不共享任何代码。 If you start adding methods to both implementations and the common interface AddComponents you might find 如果您开始向实现和通用接口AddComponents添加方法,则可能会发现

  • that you must duplicate code both in Screen and SubScreen (or delegate to Screen ) and 您必须在ScreenSubScreen都复制代码(或委托给Screen ),并且
  • that you must add methods to AddComponents which make this interface badly named. 必须将方法添加到AddComponents ,从而使该接口的名称不正确。

If both screen classes are similar both on the abstract logical level and at the implementation level, then a an class AbstractScreen with two derived classed would be better. 如果两个屏幕类在抽象逻辑级别实现级别上都相似,则具有两个派生类的AbstractScreen类将更好。 To bring back pattern speak: Use a Factory Method in AbstractScreen to specialize the behaviour regarding the different buttons. 让模式说话恢复原状:在AbstractScreen使用Factory方法专门化有关不同按钮的行为。

On your current code there is one strange thing: Why is there a method addButton defined anyway? 在您当前的代码中,有一件事很奇怪:为什么仍然定义了方法addButton Simply add the buttons in the appropriate constructor, since the user has to call addButtons in any case and the method does not have arguments. 只需在适当的构造函数中添加按钮,因为无论如何用户都必须调用addButtons且该方法没有参数。

Another point not explained is this: SubScreen has a reference to Screen which is not used. 没有解释的另一点是: SubScreen引用了未使用的Screen Why? 为什么? Will there be more method in all involved classes Screen , SubScreen and AddComponents ? 在所有涉及的类ScreenSubScreenAddComponentsSubScreen会有更多方法? Will each method be a in SubScreen delegate to Screen or only half of them? 每种方法是SubScreen中的Screen委托还是仅其中一半?

You see - there are many possibilities we do not know and are not shown in the example code but are very important. 您会看到-有很多我们不知道的可能性,示例代码中未显示,但非常重要。 I'm sure that your head contains a lot of details saying "This proposed stuff will not work because I want to do this and that one way or the other in the near future." 我确信您的头上有很多细节,上面写着“建议的内容将无法正常工作,因为我想在不久的将来以一种或多种方式进行操作。” Sadly we cannot get the content of your head into this site without more writing. 遗憾的是,如果没有更多的文字说明,我们将无法使您的想法进入该站点。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM