且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在不传入构造函数的情况下,将一个类的实例用于另一个类的***方法是什么?

更新时间:2023-09-21 16:32:46

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



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



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


$ 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.