[ACCEPTED]-Design Patterns used in WPF-design-patterns

Accepted answer
Score: 28

I've written the article about some of them: WPF and Silverlight design patterns

Here 15 is a brief description of the patterns:

1) MVVM - used 14 as a model converter and as a replacement 13 of the code-behind. Improves testability, it 12 is much easier to write unit tests for ViewModel.

2) Dependency Injection - used 11 for improving testability of a class (you 10 can write unit tests for a specific class 9 separately from others) and for the possibility 8 to change implementation in easier way (change 7 a logger, cache provider, web service etc)

3) Command - can 6 be applied to Button and MenuItem controls 5 by default, disables controls if an action 4 can't be executed. Also used in MVVM pattern 3 as a replacement of code-behind events.

Other 2 patterns from the classic book which are 1 already used in WPF:

  • Singleton. The Application class in WPF and the HttpContext class in Web forms.
  • Adapter. The data-binding engine, which uses the IValueConverter interface to convert binding values for the UI.
  • Decorator. The Border class, which decorates any UIElement class with a border of variable thickness and color.
  • Façade. The PrintDialog class, which provides a simple interface that enables you to use the entire printing and document subsystem that WPF provides.
  • Command. The ICommand interface, which is implemented by the RoutedCommand and RoutedUICommand classes.
  • Iterator. The IEnumerator interface, which many collections and lists in the .NET Framework implement.
  • Observer. The INotifyPropertyChanged interface and events.
Score: 1

Setter dependency injection:

When using MVVM you 3 have to inject an instance of ViewModel 2 into the View by setting it to the view.DataContext 1 property:

var viewModel = new CustomViewModel();
var view = new CustomView();
view.DataContext = viewModel;
Score: 1

Data binding (between View and ViewModel) uses 3 the Observer pattern. Also: the Factory 2 pattern can be used to instantiate the ViewModel 1 but that is optional.

More Related questions