7
Null Object Pattern Simplicity in doing nothing

Null object pattern

Embed Size (px)

Citation preview

Page 1: Null object pattern

Null Object PatternSimplicity in doing nothing

Page 2: Null object pattern

What is a Design Pattern?

● A reusable solution to a common problem● Kent Beck and Ward Cunningham applied to

computer science in 1987● Defined in Design Patterns: Elements of

Reusable Object-Oriented Software by GoF

Page 3: Null object pattern

Null Object Pattern

● Categorized as behavioral pattern● Similar to the strategy, or state pattern● Polymorphism● null interface that can be used by a

collaborator

Page 4: Null object pattern

When To Use

● Symptoms include null checks on properties● Conditional logic to display an object● Use of a placeholder

Page 5: Null object pattern

Demo

Page 6: Null object pattern

● Bespoke: lib/search/solr/empty_result_set.rb● Cheetah:

lib/cheetah/messenger/null_messenger.rb● Display_case: lib/display_case/exhibit.rb

(null decorator)● Factory_girl: lib/factory_girl/null_factory.rb

Examples in our Code

Page 7: Null object pattern

Extra Reading

Blog post on Factory Girl NullFactory class: http://robots.thoughtbot.com/design-patterns-in-the-wild-null-object