1 of 12

The Decorator

Application of a design pattern

Christopher Thomas�Sr. Web Developer - VirPack

2 of 12

Wait, what's a Design Pattern?

Quoth Wikipedia:

"A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design"

So it's like a blueprint.

3 of 12

Ok, so what's a Decorator?

"The decorator pattern is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class"

4 of 12

So it's one of those Russian Dolls?

Yes, and they're called Matryoshka. Bonus points if you know why Khrushchev is pictured with corn.

5 of 12

Validation is hard

(and it gets harder the more knobs and twiddly bits you incorporate)

6 of 12

Why validation is hard

Multiple...

  • Steps
  • Error conditions

Difficult to...

  • Test
  • Re-Use

I was going to put some example of password validation code here but it ended up being 53 lines long and I couldn't fit it on the slide - which sort of illustrates the point.

7 of 12

Validation by Decoration

  • Stackable validators can be individually tested.
  • Individual validators can be combined to suit specific needs
  • Individual validators can be easily reused.

8 of 12

Open/Closed Principle

"Software entities should be open for extension but closed for modification."

Additional validation features can be added without altering existing ones.

9 of 12

Ok, but how does that work?

  • Each validator returns its own result and the result of its base.Validate() method.
  • The abstract decorator's Validate() returns the Validate() routine for the next layer.
  • StringValidator's Validate() routine returns "true".

10 of 12

*The fine print

  • Complex validation is possible at the risk of invoking nested validation stacks.
  • Decoration based validation wraps the object so object type tests are failure prone.
  • Be careful with your boolean logic and order; short-cut "ands" can halt validation at the first failure.

11 of 12

MOAR PATTERNS!

Singleton

Factory

State

Iterator

Property (get, set)

Adapter

Facade

Strategy�Etc

12 of 12

Questions?

Comments? Ideas? Thoughts?�Why not have another cookie?