This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

why passing this in the contructor?  RSS feed

 
Ranch Hand
Posts: 481
10
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?
 
author
Sheriff
Posts: 23587
138
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: 481
10
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: 23587
138
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: 481
10
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!