Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在特定情况下避免Java类之间依赖关系的最佳实践?_Java_Swing - Fatal编程技术网

在特定情况下避免Java类之间依赖关系的最佳实践?

在特定情况下避免Java类之间依赖关系的最佳实践?,java,swing,Java,Swing,我花了太多的时间来弄清楚如何避免类之间的依赖关系。也许你们已经详细阐述了一些最佳实践 为了解释我的意思,让我们假设如下: 我有一个JPanel“topPanel”,其中包含一个JTextField“textField”和一个定制面板JCustomPanel“customPanel” 比方说,JCustomPanel只是有两个JButtons“button1”和“button2”,可以按下它们来更改文本字段 这就是疼痛开始的地方。只要我不希望button1和button2与textField交互,

我花了太多的时间来弄清楚如何避免类之间的依赖关系。也许你们已经详细阐述了一些最佳实践

为了解释我的意思,让我们假设如下:

  • 我有一个JPanel“topPanel”,其中包含一个JTextField“textField”和一个定制面板JCustomPanel“customPanel”
  • 比方说,JCustomPanel只是有两个JButtons“button1”和“button2”,可以按下它们来更改文本字段
  • 这就是疼痛开始的地方。只要我不希望button1和button2与textField交互,我就有一个非常独立的JCustomPanel类,我可以在任何地方使用它。编辑文本字段的最佳方式是什么?我更喜欢JCustomPanel不需要了解其topPanel的解决方案

    问题的最新情况: 我的问题得到了很好的回答,我觉得MVC模式可能真的是我问题的一个强大解决方案。我仍然有困难的地方是如何在代码中实现MVC模式。答案似乎暗示了不同的方法,所以我将发布一些代码以获得MVC模式的最佳实现

    public class JTopPanel extends JPanel {
    
      private JTextField textField;
      private JCustomPanel customPanel;
      private boolean myBool = true;
    
      public JTopPanel() {
    
       super();
       this.setLayout(new BorderLayout(5,5));     
       textField = new JTextField("0", 15);
       textField.setEditable(false);
       customPanel = new JCustomPanel();
       this.add(BorderLayout.NORTH, textField);
       this.add(BorderLayout.CENTER, customPanel);
    
     } // Constructor JTopPanel
    } // Class JTopPanel
    
    现在是JCustomPanel类:

    public class JCustomPanel extends JPanel {
    
    private JButton button1, button2;
    
    public JCustomPanel() {
        super();
        this.setLayout(new FlowLayout(FlowLayout.TRAILING, 1, 1));
        button1 = new JButton("1");
        button2 = new JButton("2");
        this.add(button1);
        this.add(button2);
        button1.addActionListener( ... ); // change textField from JTopPanel
        button2.addActionListener( ... ); // change textField from JTopPanel
        } // Constructor JCustomPanel
     } // Class JCustomPanel
    
    谢谢

    在这种情况下(“JCustomPanel不需要知道关于它的topPanel的任何信息”),topPanel应该注册到“customPanel”的按钮中,因为按下按钮时,要更改的是您的“textField”

    JCustomPanel应该有一些访问器方法,比如
    getButton1()
    getButton2()
    ,可以用来访问它们并适当地注册操作侦听器。在这种情况下,JCustomPanel将保持独立于周围世界。

    一般回答: 查看模型-视图-控制器模式(或类似设计模式)以解耦接口组件:

    更具体的回答是:

    引入一个实现接口的“控制器”类,例如ActionListener

    使用按钮(button1.addActionListener(myController))注册控制器类

    在控制器中实现接口(例如actionPerformed)以处理


    请参见

    按下按钮1或按钮2时要执行的代码取决于JCustomPanel职责范围之外的组件。 在这种情况下,您可以定义如下的接口

    public interface OnButtonClickListener {
       public void onClick(int buttonnumber);
    }
    
    在JCustomPanel中,添加一个方法来传递此接口的实现:

      public class JCustomPanel {
        private OnButtonClickListener listener;
    
        public void setOnButtonClickListener(OnButtonClickListener listener) {
          this.listener=listener;
        }
        // .. the rest of the class
      }
    
    同样在JCustomPanel中,为2个按钮创建监听器,并在此调用listener.OnClick()方法


    这样,JCustomPanel与其外部上下文没有硬依赖关系。

    将模型和视图端分开

    然后,您的文本字段将显示模型的某个值。按下按钮1或2将更新模型,而不是直接更新文本字段

    每个视图负责对模型更新作出反应。在上面的示例中,当模型中的值更改时,textfield应该得到更新。这里需要注意的是,视图并不关心谁在更新模型。它不知道按钮1/2。它只检测到模型的更新,并做出相应的反应

    这不会删除所有依赖项。最终,视图类(两个面板)和模型之间存在依赖关系。但是,两个视图类之间丑陋的依赖关系已被删除


    更多信息可以在维基百科和文章中找到。

    设计模式可能会对您有所帮助。要获得清晰的图片,请分享您的代码。+1回答很好。作为一个例子,我们可以考虑(View),它基本上是一个带有两个按钮和一个格式化文本字段和(模型)的面板。