Post by account_disabled on Dec 6, 2023 3:39:14 GMT -5
Electric duck with interface As you can see the delegate solution allowed us to get rid of problems with incorrect inheritance. At the same time the code responsible for the basic behavior of the duck exists only in one place in the delegate class. Is it a success? Unfortunately not yet! Our joy is spoiled by the transfer methods in the Duck and ElectricDuck classes. We need to write them so that the appropriate method in the delegate is called. What's worse we will have to create these methods in each class representing a duck so it doesn't look too good.
Did his uncle exchange the ax for a stick? To solve this problem we will have to modify the code a bit and call on the wellknown Lombok to the rescue Lombok to the rescue The Email Marketing List problem with forwarding methods actually comes from the fact that the Java language itself does not support the delegation mechanism unlike languages such as Groovy or Kotlin which remain in the JVM world. We also know that such language deficiencies are often patched in a quite reasonable way by the Lombok library to mention for example setters and getters. That's the case this time but there is a catch.
Lombok offers the Delegate annotation which solves our problem. However it should be borne in mind that it is treated as experimental. This means it may disappear in the future. It's not an ideal situation but it's good! To use this annotation we need to ensure that all forwarding methods contain no logic beyond calling the corresponding delegate method. In the Duck class this is not a problem because it already happens. However in the ElectricDuck class the forwarding methods perform an additional check if the batteries are inserted.
Did his uncle exchange the ax for a stick? To solve this problem we will have to modify the code a bit and call on the wellknown Lombok to the rescue Lombok to the rescue The Email Marketing List problem with forwarding methods actually comes from the fact that the Java language itself does not support the delegation mechanism unlike languages such as Groovy or Kotlin which remain in the JVM world. We also know that such language deficiencies are often patched in a quite reasonable way by the Lombok library to mention for example setters and getters. That's the case this time but there is a catch.
Lombok offers the Delegate annotation which solves our problem. However it should be borne in mind that it is treated as experimental. This means it may disappear in the future. It's not an ideal situation but it's good! To use this annotation we need to ensure that all forwarding methods contain no logic beyond calling the corresponding delegate method. In the Duck class this is not a problem because it already happens. However in the ElectricDuck class the forwarding methods perform an additional check if the batteries are inserted.