Here we take a look at the Special Case Pattern and see how it can be used to drastically simplify some typical logic around logged-in and anonymous users. Instead of representing an anonymous session as a nil value, we create a GuestUser class and identify it as a special case, which helps to make our code much simpler and more readable. The Special Case pattern is based on a broad observation that any time a program keeps switching on the same condition over and over again it's a good indication that there is a new kind of object waiting to be discovered.
Avdi is the creator of RubyTapas, a twice-weekly screencast series covering a wide variety of intermediate to advanced Ruby concepts and techniques, as well as core Object-Oriented design principles.
You need to be an enrolled student in order to view this video and subscribe.Enroll Now