Solved

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

Posted on 2014-12-18
4
81 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 74

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 20

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now