Created
September 6, 2012 04:50
-
-
Save kinopyo/3651371 to your computer and use it in GitHub Desktop.
Null Object Pattern example, extract from "Ben Orenstein - Refactoring from Good to Great, Boston Ruby August 2012" presentation. http://bostonrb.org/presentations/refactoring-a-live-coding-odyssey
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
require "ostruct" | |
class JobSite | |
attr_reader :contact | |
def initialize(location, contact) | |
@location = location | |
@contact = contact | |
end | |
def contact_name | |
if contact | |
contact.name | |
else | |
'no name' | |
end | |
end | |
def contact_phone | |
if contact | |
contact.phone | |
else | |
'no phone' | |
end | |
end | |
def email_contact(email) | |
if contact | |
contact.deliver_email(email) | |
end | |
end | |
end | |
class Contact < OpenStruct | |
def deliver_email(email) | |
# ... | |
end | |
end |
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
require "ostruct" | |
class JobSite | |
attr_reader :contact | |
def initialize(location, contact) | |
@location = location | |
@contact = contact || NullContact.new | |
end | |
def contact_name | |
contact.name | |
end | |
def contact_phone | |
contact.phone | |
end | |
def email_contact(email) | |
contact.deliver_email(email) | |
end | |
end | |
# Null Object Pattern | |
class NullContact | |
def name | |
'no name' | |
end | |
def phone | |
'no phone' | |
end | |
def deliver_email | |
end | |
end | |
class Contact < OpenStruct | |
def deliver_email(email) | |
# ... | |
end | |
end |
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
def initialize(location, contact) | |
@location = location | |
- @contact = contact | |
+ @contact = contact || NullContact.new | |
end | |
def contact_name | |
- if contact | |
- contact.name | |
- else | |
- 'no name' | |
- end | |
+ contact.name | |
end | |
def contact_phone | |
- if contact | |
- contact.phone | |
- else | |
- 'no phone' | |
- end | |
+ contact.phone | |
end | |
def email_contact(email) | |
- if contact | |
- contact.deliver_email(email) | |
- end | |
+ contact.deliver_email(email) | |
+ end | |
+end | |
+ | |
+# Null Object Pattern | |
+class NullContact | |
+ def name | |
+ 'no name' | |
+ end | |
+ | |
+ def phone | |
+ 'no phone' | |
+ end | |
+ | |
+ def deliver_email | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yeah I agree with you, personally I don't see a time when I'll use it in real life, especially in Rails.
I pasted these because this approach, or so called Null Object Pattern is new to me. Kind of inspring despite how we're gonna use it :)