Lens example

In order to show the lens design pattern in practice, we will create a class hierarchy that is usually seen in enterprise applications. Let's imagine that we are building a system for a library that can be used by the employees of different companies. We might end up with the following classes:

case class Country(name: String, code: String)case class City(name: String, country: Country)case class Address(number: Int, street: String, city: City)case class Company(name: String, address: Address)case class User(name: String, company: Company, address: Address)

The representation of these classes as a class diagram will look like the following figure:

The diagram is pretty clear and it doesn't need too much explanation. We basically ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.