O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

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

Decorating with Traits

You can use traits to decorate objects with capabilities—see the Decorator pattern in Gamma et al.’s Design Patterns: Elements of Reusable Object-Oriented Software [GHJV95]. This pattern can help keep the inheritance hierarchy relatively flat while providing reasonable extensibility. We’ll explore this pattern, and how traits play a vital role, with an example.

Suppose we want to run different checks on an applicant—credit, criminal records, employment, and so on. We’re not interested in all the checks all the time. An applicant for an apartment may need to be checked for credit history and a criminal record. On the other hand, an applicant for employment may need to be checked for a criminal record and previous employment. ...

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