Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VbScript to countdown to New Year's Day 6 54
java jdbc batch example 8 29
hibernate insert example 13 28
How to execute a Python program and gather return output in Java 2 25
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

813 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

12 Experts available now in Live!

Get 1:1 Help Now