epool
epool

Reputation: 6819

How to add a new parameter to a highly coupled method?

I have a Java class with a method highly coupled in my app API, something like this:

public class ProductModel {
    public static Product createProduct(ProductType productType, String comment) {
        return createProduct(productType, comment, null);
    }

    public static Product createProduct(ProductType productType, String comment, Long sessionTrackingId) {
        // Here now need sessionTrackingId Long
        // But this method is never called
        ....
    }
}

The first method is called in many classes and in my API project (business) and in my app project (frontend). The second method is just called in the same class ProductModel, but now I need to do a kind of refactor to use this second method by passing the sessionTrackingId that I'm getting from the app project(frontend).

The API is another project used like a Java library .jar and I need pass this parameter to the second method.

How can I do this? Maybe adding a new abstract class to the interface in each call of the first method?

Upvotes: 1

Views: 150

Answers (3)

epool
epool

Reputation: 6819

Due to the fact that this method is highly coupled, I solved this problem using the singleton pattern and setting up this value when the session starts and just using it in the method calls:

public class ProductModel {
    public static Product createProduct(ProductType productType, String comment) {
        return createProduct(productType, comment, Application.getSessionTrackingId());
    }

    public static Product createProduct(ProductType productType, String comment, Long sessionTrackingId) {
        // Here now need sessionTrackingId Long
        // But this method is never called
        ....
    }
}

Upvotes: 0

Pete B.
Pete B.

Reputation: 3276

This kind of thing falls within the realm of a facade facade pattern. And is good practice. The key is to keep as much code as you can in common between the method signatures. Your description of the problem is a bit hard to interpret, it is not clear that you are actually trying to add a third parameter as suggested in your title.

I mostly agree with Carl. You would add method signatures while defaulting unsupplied parameters. Please realize that "inlining" is not a developer responsibility in Java, it is left for the JVM.

Upvotes: 0

Carl Manaster
Carl Manaster

Reputation: 40336

I would simply inline the first method, everywhere it is called. Now your callers are all calling the second method, with a null third parameter. Find everywhere it's called and replace the null with whatever is appropriate in the calling context.

Upvotes: 1

Related Questions