The bridge design pattern the Scala way

The bridge design pattern is another example of those that can be achieved with the powerful features of the Scala programming language. Here, we will be using self types. The initial Hasher trait remains unchanged. Then, the actual implementations become traits instead of classes as follows:

trait Sha1Hasher extends Hasher {  override def hash(data: String): String =     new String(Hex.encodeHex(getDigest("SHA-1", data).digest()))}trait Sha256Hasher extends Hasher {  override def hash(data: String): String =     new String(Hex.encodeHex(getDigest("SHA-256", data).digest()))}trait Md5Hasher extends Hasher {  override def hash(data: String): String =     new String(Hex.encodeHex(getDigest("MD5", data).digest())) ...

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.