1.Topics‎ > ‎Architecture‎ > ‎Patterns‎ > ‎a.Design Patterns‎ > ‎C.Structural Patterns‎ > ‎

M.Special Case

  1. Motivation
    1. A subclass that provides special behavior for particular cases.
  2. Summary
    1. it is actually replacement of null values that defeat polymorphism.
    2. It helps to avoid null checks and makes programming extremely typed.

  3. When to Use
    1. Use Special Case whenever you have multiple places in the system that have the same behavior after a conditional check for a particular class instance, or the same behavior after a null check.
  4. Overview Tutorials
    1. http://martinfowler.com/eaaCatalog/specialCase.html
  5. Implementation
    1. The basic idea is to create a subclass to handle the Special Case. Thus, if you have a customer object and you want to avoid null checks, you make a null customer object.
    2. Take all of the methods for customer and override them in the Special Case to provide some harmless behavior. Then, whenever you have a null, put in an instance of null customer instead.
  6. Example 
    1. IEEE 754 floating-point arithmetic offers good examples of Special Case with positive infinity, negative infinity, and not-a-number (NaN). If you divide by zero, instead of getting an exception that you have to deal with, the system just returns NaN, and NaN participates in arithmetic just like any other floating point number.
  7. Related Patterns
    1. Null Object : A further variant of Special Case Pattern
  8. Related Technologies 
    1. None
  9. References
    1. None