Sri
asked on
Best Practice to introduce a new class in existing project
What is the best way to introduce a new object to existing classes, in my project i have several classes and now i am implementing reporting and have to log some messages whenever a method execution is failed
Is it a good practice to create object of the new class in all the existing classes or is there any other way?
trying to minimize the code changes
Initial set of code
Code after introducing a new Class (for Ex: Logging for reporting)
Like this i have around 30 classes and around 100 methods , is the above process a good practice to go and add each object/class or is there any best practice to simplify the process
Please suggest
Is it a good practice to create object of the new class in all the existing classes or is there any other way?
trying to minimize the code changes
Initial set of code
Class one{
public void method1() {
try
{
//perform something
}
catch(Exception ex){
S.o.p("some message")
}
}
public void method2() {
try
{
//perform something
}
catch(Exception ex){
S.o.p("some message")
}
}
}
Class Two {
public void method1() {
try
{
//perform something
}
catch(Exception ex){
S.o.p("some message")
}
}
public void method2() {
try
{
//perform something
}
catch(Exception ex){
S.o.p("some message")
}
}
}
class Test
{
public static void main(String args[]){
One o = new One();
o.method1();
o.method2();
Two t = new Two();
t.method1();
t.method2()
}
}
Code after introducing a new Class (for Ex: Logging for reporting)
Class One{
Private ReportLogger report;
Public One(Report report)
{
this.report = report;
}
public void method1() {
try
{
//perform something
}
catch(Exception ex){
report.log("method failed due to " + ex);
S.o.p("some message")
}
}
public void method2() {
try
{
//perform something
}
catch(Exception ex){
report.log("method failed due to " + ex);
S.o.p("some message")
}
}
}
Class Two {
Private ReportLogger report;
Public Two(Report report)
{
this.report = report;
}
public void method1() {
try
{
//perform something
}
catch(Exception ex){
report.log("method failed due to " + ex);
S.o.p("some message")
}
}
public void method2() {
try
{
//perform something
}
catch(Exception ex){
report.log("method failed due to " + ex);
S.o.p("some message")
}
}
}
class Test
{
public static void main(String args[]){
ReportLogger rl = new ReportLogger();
One o = new One(rl); // Modifed here
o.method1();
o.method2();
Two t = new Two(rl); // Modified here
t.method1();
t.method2()
}
}
Like this i have around 30 classes and around 100 methods , is the above process a good practice to go and add each object/class or is there any best practice to simplify the process
Please suggest
ASKER
thank you @ste5an for responding very soon
Actually class One is a kind of common class that is referred in many classes and i am trying to add another reporting (Thrid Party Reporting tool) class
Since it is for reporting i should be able to know from which class (source) the call is made from so that we have more information in the reports
below is the way i am using reporting, sorry the previous one is not the correct one
Actually class One is a kind of common class that is referred in many classes and i am trying to add another reporting (Thrid Party Reporting tool) class
Since it is for reporting i should be able to know from which class (source) the call is made from so that we have more information in the reports
below is the way i am using reporting, sorry the previous one is not the correct one
class Test1
{
public void test1()
ReportLogger rl = new ReportLogger("Source is Test1" );
One o = new One(rl);
o.method1();
o.method2();
Two t = new Two(rl);
t.method1();
t.method2()
}
}
class Test2
{
public void test2()
ReportLogger rl = new ReportLogger("Source is Test2" );
One o = new One(rl);
o.method1();
o.method2();
Two t = new Two(rl);
t.method1();
t.method2()
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Dirk Strauss, I will have to try this. This is an existing project and i am asked to inject a third party reporting tool
hmm, what's wrong with extracting those information from the stack frames?
@Sri,
Are you all set with this now, or do you need more help? If all set, could you please close it out now. If you need help with the question close process take a look at:
»bp
Are you all set with this now, or do you need more help? If all set, could you please close it out now. If you need help with the question close process take a look at:
»bp
You introduce a new ctor. Which means you need code changes(as long as you don't plan to use a DI container to inject your logger. This means you introduce a coupling for an aspect. There is no way to avoid it.
The only question is: Must it be a new ctor or would be a property sufficient?
Caveat: what ever route you take, you should obey Liskov, thus in the current state you violate it, cause you create a new possibility to raise an error, when Null is handed to the ctor. So your modified classes should look like:
Open in new window
As logging is an aspect, why can you not put it into your S.o.p() call instead?Open in new window