Explain how it lives and how to work with it?

This refers to the following phenomenon:

Strings .emptyToNull(sector)) .stream() .map(ClientCompany::fromCompany) .collect(Collectors.toList()); 
  • What is cheining? Where did you find out about him? maybe an english term? - Grundy
  • well, what terminology the teacher uses, and call it ... - Ra'Uko
  • four
    TS must be about this Method chaining - LEQADA
  • one
    Yes, everything is simple. The method, having executed its work, returns any object. Naturally, you can immediately call the method of this returned object. In particular, you can return this. Won same poked in Wikipedia. Everything is clear how it works. - Sergey
  • one
    @ zenden2k, No more confusing than with other methods, in some cases even less :-) - Grundy

3 answers 3

Quote from Wikipedia article Method chaining

Method chainig (chains) - the general syntax name in OOP, in which several methods are called one after another.

There is also a simple Java code example:

 class Person { private String name; private int age; public Person setName(String name) { this.name = name; return this; // Возвращаем объект } public Person setAge(int age) { this.age = age; return this; // Возвращаем объект } public void introduce() { System.out.println("Hello, my name is " + name + " and I am " + age + " years old."); } // Использование: public static void main(String[] args) { Person person = new Person(); // Output: Hello, my name is Peter and I am 21 years old. person.setName("Peter") .setAge(21) .introduce(); // Вызываем методы цепочкой (Method chaining) } } 

The whole idea is to make the methods be called one after the other. To do this, each previous method must return the value that the next one needs. As a Lego designer.

The structure can be made very different, up to nested (nested) classes.

It can be emphasized that the use of such syntax saves time. I do not think that it is worth using it often. Such classes with increasing complexity become unreadable.

It is worth using it only when it is very long without it.

    In Java, it is no different from other programming languages. The whole idea of ​​method chaining comes down to a simple rule:

    Methods must return objects.

    It can be absolutely any objects - as belonging to the class or parents of the class containing the method, or not. Just after exiting the method, always return the object.

    You will apply this rule - you will be able to organize pretty nice looking chains from operations like this:

     player .takeRevolver() .loadBullet() .rotateCylinder() .putToHead() .shoot(); 

    Instead of subjectively less pleasant:

     Revolver revolver = new Revolver(player); revolver.loadBullet(); revolver.rotateCylinder(); revolver.putToHead(); revolver.shoot(); 

    Also based on the idea of ​​a method chaining, there is an idea about a fluent interface, the essence of which comes down to:

    Methods must return the object to which they belong.

    This allows you to create quite nice looking software interfaces "builders", involving a multi-step configuration of the created objects:

     Car car = CarBuilder:create() .setEngine(new Engine(FuelType::GAZOLINE, 1.5)) .setExterior(Colors::BLACK_MATTE) .setInterior(Materials::GRAY_FABRIC) .setAbs(new Abs(4, 4)) .setNeon(Colors::PURPLE) .build() 

      On Habré , in my opinion, there is a good article on this topic. The truth is that its implementation in C ++ is considered there, but as far as I understand, this trick applies to java. There is also a link from @LEQADA to an English-language article on wikipedia, it’s a pity that it’s not in Russian

      • After reading the article and comments, I remembered that in pascal and even in a basic Besik there is a with operator that allows you to do the same fluent without return this ! Any method fits into the fluent! What is being presented as a revelation in reality is a dense backwardness opposite to BASIC :) - Sergey
      • @banme What I like habr for is that sometimes there is more useful information in comments, sometimes than in the article itself) - abbath0767