views:

132

answers:

2

I have been getting more involved with WPF for about a year now. A lot of things are new and sometimes it is hard to get my head wrapped around it.

At the same time I am rereading the GOF Design Patterns book.

A few times I would stop in the middle because I would realize that a certain pattern is the very one used in some WPF functionality. Whenever such a realization hits me, I feel like my understanding of the related WPF principle just took a big leap. It's kind of like an aha-effect.

I also realized that I had a much easier time understanding Prism for example because the documentation does such a great job at explaining the patterns involved.

So here is my "question" (more like an effort):

In order to help us all to understand WPF better it would be great if anyone who also "spotted" a design pattern in WPF could give a short explanation.

One pretty obvious example that I found is the Routed Event:

If an event is detected by a child control and no handler has been specified, it passes it along to its parent and so on until it is finally handled or no parent is found anymore.

Lets say we have an image on a button that is inside a StackPanel that is inside a window. If the user clicks the image, the event will either be handled by it (if handling code has been specified) or "bubble" up until one of the controls handles it. So each control will get a chance to react in this order.

  1. Image
  2. Button
  3. StackPanel
  4. Window

Once a control handles it, the bubbling will stop.

This is the short explanation, for a more precise one consult the WPF literature.

This kind of functionality represents the "Chain of Responsibility Design Pattern" which states, that if their is a request, it gets passed along a responsibility chain to give each object in it a chance to handle it. The sender of the request has no idea who will handle it which ensures decoupling. For a more thorough explanation follow the link.

The purpose here is merely to show how this (seemingly old 10+ years) idea found its way into our current technology and to offer another way of looking at it.

I think this is enough for a start and hope more parallels will be collected here.

Cheers, Thorsten

A: 

I'd say CommandBindings are pretty important and fundamental to the way I develop.

Echilon
A: 

I don't think it is specific for WPF but the observer design pattern seems to be the foundation on which all event handling in .Net and WPF is based.

The observer design pattern is described as "Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.". In .Net with the += operator you subscribe to such a change in state. Subsequently you unsubscribe with the -= operator.

kalkie