• Post Reply Bookmark Topic Watch Topic
  • New Topic

why passing this in the contructor?  RSS feed

 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!