Composition
摘要
Large software systems are assembled from smaller parts. In object-oriented design, parts are connected through two main mechanisms: composition and inheritance. Composition means that one object holds a reference to another object and delegates some functionality to it. Although this sounds straightforward, unprincipled composition can lead to a mess of spaghetti code. In this chapter I give a quick refresher on the mechanism of polymorphism and how it can be used to elegantly compose objects together by following some well-known design patterns. The second way of assembling systems is through inheritance, which is more complex and is covered in Chapter 7.