Created
February 11, 2015 05:02
-
-
Save mikepack/c2e02505ecff4acd1edc to your computer and use it in GitHub Desktop.
Polymorphic Null Object
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
class User | |
def name | |
'Mike Pack' | |
end | |
end | |
class GuestUser | |
def name | |
'Guest User' | |
end | |
end | |
class AdminUser | |
def name | |
'Admin User' | |
end | |
end | |
class Controller | |
def current_user | |
return GuestUser.new if rand(0..1) == 0 | |
return AdminUser.new if rand(0..5) == 0 | |
User.new | |
end | |
def show | |
puts current_user.name | |
end | |
end | |
Controller.new.show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment