Post by account_disabled on Dec 6, 2023 3:38:55 GMT -5
It is necessary to add methods that have previously been defined in the abstract class and implement them using a delegate . code From ducks to delegations or problems with inheritance in Java Listing Duck with delegate And we do the same in the ElectricDuck class . code From ducks to delegations or problems with inheritance in Java Listing Electric duck with delegate And that's all for now! If we run our tests they should still work. Only one change will be needed the class from which we perform static imports code From ducks to delegations or problems with inheritance in Java Listing.
The DuckTest class after a minor change It is easy to notice that composition has many advantages over classic inheritance It is much more flexible and natural it is much easier to describe the structure of objects in the system using composition than inheritance the latter Email Marketing List is often created a bit forcibly The behavior of an object can be modified while the program is running by replacing the incorporated object with another one. This is impossible with inheritance the inherited part is rigid Composition defects Of course the disadvantages of the composition should also be mentioned Memory consumption increases as we use more small objects.
The efficiency of calling methods drops slightly when calling a method on an object we actually have to call the method from the delegate it is one more frame on the stack Classes that have the same functionality and methods Duck and ElectricDuck in our case become completely disjoint in the type hierarchy which in some languages makes it impossible to simply replace them with each other. There are many methods created whose sole purpose is to call the appropriate delegate method socalled forwarding methods. The last two disadvantages depend on the language we use and do not occur in some programming languages.
The DuckTest class after a minor change It is easy to notice that composition has many advantages over classic inheritance It is much more flexible and natural it is much easier to describe the structure of objects in the system using composition than inheritance the latter Email Marketing List is often created a bit forcibly The behavior of an object can be modified while the program is running by replacing the incorporated object with another one. This is impossible with inheritance the inherited part is rigid Composition defects Of course the disadvantages of the composition should also be mentioned Memory consumption increases as we use more small objects.
The efficiency of calling methods drops slightly when calling a method on an object we actually have to call the method from the delegate it is one more frame on the stack Classes that have the same functionality and methods Duck and ElectricDuck in our case become completely disjoint in the type hierarchy which in some languages makes it impossible to simply replace them with each other. There are many methods created whose sole purpose is to call the appropriate delegate method socalled forwarding methods. The last two disadvantages depend on the language we use and do not occur in some programming languages.