Method Chaining Explained (in Computer Programming)

preview_player
Показать описание
It is common to see the 'method chaining' syntax in source code all across the world and in almost every object oriented programming language.

Also called 'named parameter idiom', it looks like a chain of methods being called one after another, hence the name 'method chaining'.

It works because the methods that are being called on the object are actually returning the SAME instance of that object, meaning another method of that object can be called right after it nice and easily.

In this video I show you an example of how you can implement method chaining in Java by creating a new class that supports it. The class is a simple number wrapper with an addition and print method.

For your reference:

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
Рекомендации по теме
Комментарии
Автор

hi, how to limit method chain to some perticular function ? returning this will give all method access, and i am trying to make a routing package where i need to return a chain of method like router.get() here i only want to return get, post and other method but i have many methods in class that all are showing up, i also have applyMiddleware() method that i only want after method is selected but it is showing while accessing router.* anything

creative-commons-videos
Автор

so what it means for ?
it looks like getcontentpane and setbackground cannot be separated but if it so than it must be some other type from chaining method i guess ?

syarifnm
Автор

where do we use this? like match it in a real world concepts??

OnlyJavascript