Approach 2b: Use an Interface

  • Make an interface for ProfileFetcher
  • Pass that interface in the constructor for TradeCalculator
  • Construct the ProfileFetcher outside of TradeCalculator
public interface ProfileFetcher {
    public ProfileData fetchProfile(String userId);
}
public class SqlProfileFetcher implements ProfileFetcher {
    // ...
}
public class TradeCalculator {    
    private final ProfileFetcher profileFetcher;
    public TradeCalculator(ProfileFetcher profileFetcher) {
        this.profileFetcher = profileFetcher
    }
}

24 / 31