[Logo]
Forums Register Login
why passing this in the contructor?
I went trough some code well written, following the so called Hollywood principle, do not call us, I will call you. In other words a code writenn with the oncle Bob principles that what is inside does not know what is outside.

and I found that the view call presenter instantiating the view with the keyword this.

example

what are the reason of passing this as argument? which advantages I get?
 

Giovanni Montano wrote:
what are the reason of passing this as argument? which advantages I get?



There nothing advantageous or disadvantageous about using the this reference. You are simply fulfilling the contract (interface) of the constructor. The constructor takes a certain type of instance, and the calling code happens to be part of the instance (either constructor or method) that is to be passed.

Henry
 

Henry Wong wrote:

Giovanni Montano wrote:
what are the reason of passing this as argument? which advantages I get?



There nothing advantageous or disadvantageous about using the this reference. You are simply fulfilling the contract (interface) of the constructor. The constructor takes a certain type of instance, and the calling code happens to be part of the instance (either constructor or method) that is to be passed.

Henry


thanks, but i do not get that I am fulfilling the contract interface of the constructor. I understand the concept you mean, but there is any interface keyword involved right?
I know is possible to define a contructor inside an interface, but i do not see any interface, is your a metaphorical way of speaking? Thanks

I think you are reading too much into my response.

By interface, I mean the signature, or the parameters. The constructor expects a certain instance to be passed as a parameter. And the calling code, happens to have a reference to it, which is itself.

There is no special feature, or use-case, here. It just happens to be accessible via this because that is how the application is written. You could have changed the algorithm (or code location) and then you would need a different reference for the parameter.

Henry
 

Henry Wong wrote:
I think you are reading too much into my response.

By interface, I mean the signature, or the parameters. The constructor expects a certain instance to be passed as a parameter. And the calling code, happens to have a reference to it, which is itself.

There is no special feature, or use-case, here. It just happens to be accessible via this because that is how the application is written. You could have changed the algorithm (or code location) and then you would need a different reference for the parameter.

Henry


Ok thanks

This thread has been viewed 233 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 18, 2018 22:24:58.