Solved

Should one define a variable with new() in a class or should one inject it

Posted on 2014-12-18
4
85 Views
Last Modified: 2015-02-10
Hi,
I have a class :
public class AuthenticationManager {

    private static final Logger _logger = LoggerFactory.getTrimmer(AuthenticationManager.class.getSimpleName());
    private final List<Listener> _listeners = new ArrayList<Listener>();
    private boolean _verificationInProgress = false;
    private final CallStateReceiver callStateReceiver = new CallStateReceiver();


I was wondering is it a good practice to have the line :
    private final CallStateReceiver callStateReceiver = new CallStateReceiver();

or should one chnage it to private final CallStateReceiver callStateReceiver ; and get it set from outside ?

What way can we define this ?

Thanks
0
Comment
Question by:Rohit Bajaj
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40506851
If you instantiate the object directly within the class, then you introduce a dependency on the "new" object within the class. This means your class is tightly coupled with your "new" object. A more flexible design, as you've mentioned, would be to inject the instantiated object into the class, either via the constructor or a method. Even better, you might define an interface that lays out the minimum criteria that your class expects from an object. Then you work with the interface type internally. You don't have to know what kind of object you actually have as you know what to expect from your object. In this way, different callers can have different object types, but they can each still work with your class because they each meet the minimum criteria set forth by your interface definition.
0
 
LVL 21

Expert Comment

by:Amitkumar Panchal
ID: 40506901
Agree with kaufmed.

If you instantiate the object inside the class, your code is tightly coupled and dependent on the objects created using new(). Best approach is, as suggested by kaufmed, to provide the objects from outside.

Consider a case in future if you have another flavor of CallStateReceiver (say MyCallStateReceiver) and you want to use it in place of CallStateReceiver. How will you do it ? It is not possible if you instantiate the object inside the class, you will need to have mechanism (ie. setter or constructor) where objects can be provided outside the class.

Hope the explains.
0
 

Author Comment

by:Rohit Bajaj
ID: 40508860
HI,
I did not understand the point -
Consider a case in future if you have another flavor of CallStateReceiver (say MyCallStateReceiver) and you want to use it in place of CallStateReceiver. How will you do it ? It is not possible

MyCallStateReceiver will also have the object inside it.. how is it not possible. please clarify exactly what you mean.

thanks
0
 

Author Comment

by:Rohit Bajaj
ID: 40508879
Hi,
Can you please give an example of :
 Even better, you might define an interface that lays out the minimum criteria that your class expects from an object.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question