Post by account_disabled on Dec 6, 2023 0:39:20 GMT -8
So we need to externalize these checks using the strategy pattern and pass such strategies to the delegate. In our case these will be just two Runnable fields representing a block of code to execute before voting and attempting to flow . code From ducks to delegations or problems with inheritance in Java Listing Delegate with strategies Additionally we added a constant representing an empty strategy that does nothing. This will make it easier for us to work on the Duck class in a moment. Using the Delegate annotation is simple just use it on the field representing the delegate. Then we remove the forwarding methods and that's it! The Duck class will look like this.
From ducks to delegations or problems with inheritance in Java Listing Duck class with Delegate annotation It is then easy to modify the ElectricDuck class . code From ducks to delegations or problems with inheritance in Java Listing ElectricDuck class with Delegate annotation We can breathe a sigh of relief because the resulting code looks much better! We can Email Marketing List now look at the results of our work and try to go through all the steps again. Time to sum up As the example presented shows us when creating a hierarchy of objects in Java it is relatively easy to make a mistake when inheriting from an existing class. It is true that the Liskov substitution principle should protect us against this but unfortunately it is easy to break it unconsciously.
Therefore instead of simple inheritance it is worth considering other approaches such as introducing an abstract class simpler or replacing inheritance with composition more complex but more flexible. When using composition you should also remember that due to strong type control in Java you need to create an interface containing common class methods so that in the future you will not be blocked from replacing an object of one type with an object of another type.
From ducks to delegations or problems with inheritance in Java Listing Duck class with Delegate annotation It is then easy to modify the ElectricDuck class . code From ducks to delegations or problems with inheritance in Java Listing ElectricDuck class with Delegate annotation We can breathe a sigh of relief because the resulting code looks much better! We can Email Marketing List now look at the results of our work and try to go through all the steps again. Time to sum up As the example presented shows us when creating a hierarchy of objects in Java it is relatively easy to make a mistake when inheriting from an existing class. It is true that the Liskov substitution principle should protect us against this but unfortunately it is easy to break it unconsciously.
Therefore instead of simple inheritance it is worth considering other approaches such as introducing an abstract class simpler or replacing inheritance with composition more complex but more flexible. When using composition you should also remember that due to strong type control in Java you need to create an interface containing common class methods so that in the future you will not be blocked from replacing an object of one type with an object of another type.