concept code duplication in category ethereum

This is an excerpt from Manning's book Building Ethereum Dapps: Decentralized applications on the Ethereum blockchain.
The purpose of the previous chapter was to give you a foundation in Solidity, so I focused mainly on the basic syntax that the language offers. In the next two chapters, I’ll introduce more advanced object-oriented (OO) features. I’ll start with inheritance in chapter 6 and follow with abstract contracts and interfaces in chapter 7. These OO features allow you to reduce code duplication and make your contracts more composable.
As I noted, SimpleCoin and SimpleCrowdsale use the same onlyOwner modifier to restrict access to some operations to only the contract owner. The advantage is that you’re using onlyOwner consistently across the two contracts. The downside is that you had to introduce the owner state variable and implement the onlyOwner modifier in both contracts. Wouldn’t it be nice to place this modifier somewhere so you could then drop it into both SimpleCoin and SimpleCrowdsale without introducing code duplication? Fortunately, you can by encapsulating the ownership state and onlyOwner modifier into a separate contract, called Ownable, as shown in the following listing.
Figure 7.4. Reorganized crowdsale contract hierarchy, with two layers of abstract contracts at the top and a bottom layer of concrete contracts encapsulating all combinations of the contract pricing and funding limit options. The FundingLimitStrategy contract hierarchy now encapsulates the checks on the funding limit in an efficient way that avoids code duplication.
![]()