O'Reilly logo

Scala Design Patterns by Ivan Nikolov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The composite design pattern

The composite design pattern is used to describe groups of objects that should be treated the same way as a single one.

Note

Its purpose is to compose objects into tree structures to represent whole-part hierarchies.

The composite design pattern is useful for removing code duplication and avoiding errors in cases where groups of objects are generally treated the same way. A popular example could be a file system in which we have directories, which can have other directories or files. Generally, the interface to interact with directories and files is the same, so they are good candidates for a composite design pattern.

Class diagram

As we mentioned previously, file systems are a good candidate for the composite design pattern. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required