From where to get RestTemplate to call getPortfolioManager(...)

In order to call function getPortfolioManager(RestTemplate restTemplate) of PortfolioManageFactory ; I need a rest template, but on inspecting every function none of them return a rest template , so am I supposed to create a new rest template object and pass to it, ??
@akashchhetri @Rahul-Crio.do

You need to return an object of ProjectManagerImpl which takes restTemplate as parameter from Factory method.

I understand the returning part , according to the code given to us

return portfolioManager.calculateAnnualizedReturn(Arrays.asList(portfolioTrades), endDate);
}

I need to get an object of portfolioManger which I can get from getPortfolioManager(RestTemplate restTemplate), but to call this method I need to pass it a rest template , where to get that ???
@akashchhetri

restTemplate is instance variable use directly as restTemplate inside PortfolioManagerImpl

okay I can create a new one and use it right??

To answer your original question, yes, you need pass a restTemplate to factory which will give you a manager object which you need to use to call further methods.

since hard coding can create problem and on observing I didn’t find one so, just clarifying :sweat_smile: :sweat_smile:

See the scenario is

here can you see PortfolioManagerServiceImpl it has one attribute and one function you can directly use that attribute inside the calculateAnnualizedReturn to get your answer

It’s usually passed to the function you are calling it from, otherwise feel free to initialise it.

1 Like

In the function : public static List mainCalculateReturnsAfterRefactor(String[] args)

whose return statement is : return portfolioManager.calculateAnnualizedReturn(Arrays.asList(portfolioTrades), endDate);
}
I need a portfolioManager instance,
I can get that from function defined in portfolioManagerFactory , but that function :
getPortfolioManager(RestTemplate restTemplate),

requires template which is not given to me from anywhere,
So I need to create one on my own, Hope I am going in the right direction.

1 Like

restTemplate is a class variable for PortfolioManagerServiceImpl which must be initialize in constructor of PortfolioManagerServiceImpl. Now visibility of a class-level variable
image
is YES for all the cases. So simply we can use them anywhere inside our class.

Now if we talk about getPortfolioManager() this function will not be used by you It is for client-side please refer above diagram. So don’t bother about this method.

1 Like

thanks got it now, but actually this statement : portfolioManager.calculateAnnualizedReturn(Arrays.asList(portfolioTrades), endDate);
}
was giving me error because of portfolioManager word so I thought only that is the function which can return instance of portfolioManger, so I was asking for restTemplate if that is the case I am just creating an instance afresh of the reqeuired type and proceeding , thanks for putting so much effort :slight_smile:

@akashchhetri I am totally exhausted after thingking how to remove error from this line
portfolioManager.calculateAnnualizedReturn(Arrays.asList(portfolioTrades), endDate);
}
If I am not using factory method than I have to give it refernce of implementing class, and that implementing class too has a parametrised constructor accepting RestTemplate as argument, so what is the way out, just leaving this function won’t work becasue it is erraneous line, also while creating object of PortfolioManagerImpl in
PortfolioManagerApplication I am getting error so how to get rid of these , stuck for more than required please help here @akashchhetri

Yes you have to create the instance of RestTemplate of your own.

2 Likes

and then as given in the TODOs - Create PortfolioManager using PortfoliomanagerFactory