Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

java - What is best way to use instance of one class into another class without passing into constructor

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);   
                }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

is there any way to use instance of one class into another class without passing instance into constructor?

There are other ways.

  • Pass the instance of the second class to the first class using a setter method.
  • Pass the instance of the second class to the first class by assigning to a instance variable in the first class.
  • Create the instance of the second class in the first class.

If you an answer that is more relevant to your example, you will need to explain more clearly what you are trying to do here, and why you think that your current solution is unsatisfactory.


Re this attempted explanation:

I have to follow chain of inner class and pass same instance to reach specific class. I want to avoid it.

I have no idea what you are trying to say. I suspect that other readers has the same problem.

I suspect that the real problem here is with the design of your existing code. It looks like you / someone has gone a bit crazy with nesting classes, and that you are suffering the consequences. It could be that the only way to simplify things is to unpick the nesting or rethink the constructors. (Why does a private constructor require a "builder" argument?)


It is a fairly common phenomenon for complicated OO software to have inherently complicated initialization patterns. There tends to be no neat way to deal with this programatically, but you can often avoid this by using some kind of "Dependency Injection" (DI) mechanism. Another name for this is "Inversion of Control" (IoC).

For example, Spring DI works by adding annotations to your class, and getting Spring to create and assemble the instances in the required form. Or you can specify how the instances (beans) are assembled in XML.

This could be a solution for you ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...