在不传入构造函数的情况下,将一个类的实例用于另一个类的最佳方法是什么? [英] What is best way to use instance of one class into another class without passing into constructor

查看:184
本文介绍了在不传入构造函数的情况下,将一个类的实例用于另一个类的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果两个类都处于同一级别(都是子类),那么如何将一个类的实例用于另一个类。将一个类的实例用于另一个类而不传递给构造函数的最佳方法是什么? / strong>因此手动要求传递null。如何制作独立的代码?

 类PreviewPanel {

私人预览面板(Builder生成器){
this.previewMode = builder.previewMode;

formsPreview = new NTFormsPreview(previewMode);
formsPreview.setCanAddComment(builder.canAddComment);

ntPreviewTreePanel = new NTPreviewTreePanel(builder,formsPreview);
//这个类有一些事件总线的实现。有时候,formsPreview实例是必需的。

}
public static class Builder {
private PreviewMode previewMode;

私人文档文档;
public Builder(PreviewMode previewMode,Document document){
this.previewMode = previewMode;
this.document = document;
}
public PreviewPanel build(){
return new PreviewPanel(this);
}

}
}

如果我将该实例传递给构造函数,我必须遵循内部类的链并通过相同的实例到达特定的类。我想避免它。这是大产品。要显示实际的处理程序执行情况并不容易。


$ b 代码结构:

  private PreviewPanel(Builder builder)
- > formsPreview = new NTFormsPreview(previewMode);

- > NTPreviewTreePanel(builder,formsPreview);
- > NTPreviewTree(文档,bidDocuments,previewMode,canAddComment,canViewComment,previewFormTxnEncryptionDetails,formsPreview);
- > NTTreeNode(formsPreview)
private void fireReportItemClicked(Document document,esenderCSReport){
eventBus.fireEventFromSource(formPreviewEvent,formsPreview);
}


解决方案

您的问题对我来说还不清楚但我认为你可以做到以下几点:



创建一个新的类,它将拥有你的两个子类 - 组合



让它工作一个中介模式,这样你就可以在你想要的规则/逻辑之后的另一个中使用和调用东西。


$ b $ ol
  • 不需要以任何方式将其中一个孩子传递给另一个孩子。

  • 自由地重新设计交互逻辑,如果它发生变化

  • 没有必要改变你已经拥有的结构

    供参考建造者似乎与建造者模式有关,你可能想阅读它,看看你是否能理解你的项目中的某些东西。


    If both classes are at same level (Both are child class), how to use instance of one class into another one.What is best way to use instance of one class into another class without passing into constructor? so manually require to pass null. How to make independent code?

        Class PreviewPanel{
    
        private PreviewPanel(Builder builder) {
            this.previewMode=builder.previewMode;
    
            formsPreview=new NTFormsPreview(previewMode);
            formsPreview.setCanAddComment(builder.canAddComment);
    
            ntPreviewTreePanel=new NTPreviewTreePanel(builder,formsPreview);
        //This class have some event bus implemented.Sometime There, formsPreview instance is require.
    
               }
        public static class Builder {
            private PreviewMode previewMode;
    
            private Document document;
            public Builder(PreviewMode previewMode,Document document) {
                this.previewMode = previewMode;
                this.document=document;
            }
            public PreviewPanel build() {
                  return new PreviewPanel(this);
            }
    
         }
    }
    

    If I pass that instance into constructor,I have to follow chain of inner class and pass same instance to reach specific class. I want to avoid it. This is big product. it is not easy to show how many classes inside it to reach actual handler implementation.

    Code Structure:

    private PreviewPanel(Builder builder)
        ->formsPreview=new NTFormsPreview(previewMode);
    
            ->NTPreviewTreePanel(builder,formsPreview);
                     ->NTPreviewTree(document, bidDocuments, previewMode, canAddComment,canViewComment, previewFormTxnEncryptionDetails,formsPreview);
                 ->NTTreeNode(formsPreview)
                    private void fireReportItemClicked(Document document,esenderCSReport){
                                    eventBus.fireEventFromSource(formPreviewEvent, formsPreview);   
                    }
    

    解决方案

    Your problem is unclear to me but I think you could do the following :

    Create a new Class that will "Own both of your child class - composition"

    Make it work a Mediator Pattern so that one can use and call stuff on the other one following the rule/logic you want.

    1. No need to pass one of the child to the other one in any way.
    2. Freely, redesign the interaction logic if it gets to change
    3. No need to "change"" the structure you already have

    FYI The builder, seems to be related to the Builder Pattern, you might wanna read on it and see if you can understand something out of your project.

    这篇关于在不传入构造函数的情况下,将一个类的实例用于另一个类的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆