Created
April 28, 2011 17:43
-
-
Save k4200/946845 to your computer and use it in GitHub Desktop.
inheritance or mix-in?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// There's a class called MappedPassword in Lift. I wanted to create a class that | |
// holds the plain text when a password is set. | |
//------------------------------- | |
// This works, but I didn't like the idea for two reasons. | |
// 1. The class name is long. | |
// 2. Seems kinda ugly to me. | |
abstract class MappedPasswordWithPlainWhenSet[A<:Mapper[A]](override val fieldOwner: A) | |
extends MappedPassword[A](fieldOwner) { | |
var plain: String = "" | |
override def set(value: String): String = { | |
plain = value | |
super.set(value) | |
} | |
} | |
// In a model class (User) | |
override lazy val password = new MyPassword2(this) | |
protected class MyPassword2(obj: User) | |
extends MappedPasswordWithPlainWhenSet(obj) { | |
override def displayName = fieldOwner.passwordDisplayName | |
} | |
//------------------------------- | |
// I wanted to make it a trait, but the following code doesn't work. | |
trait HoldsPlainWhenSet[A<:Mapper[A]] { | |
self: MappedPassword[A] => | |
var plain: String = "" | |
override def set(value: String): String = { | |
plain = value | |
// self.super.set(value) // This doesn't compile. | |
// self.set(value) // This causes an infinite loop. | |
} | |
} | |
// In a model class (User) | |
override lazy val password = | |
new MyPassword(this) with HoldsPlainWhenSet[User] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment