Strategy and Decorator Pattern


It’s been a long time since I posted anything, been very busy with work. Many things have changed and I am currently working as an independent contractor.  My latest gig at the moment is working on a WPF product! 🙂 woo hoo!

Today I’m going to talk about using the Strategy and Decorator Pattern together, as it turns out they work pretty well together. Strategy pattern is a good way to encapsulate your algorithms, or in my case I want to use it to encapsulate reusable code, following the principle to keep it DRY. With the introduction of Action and Func classes in .NET framework, I have found that writing callbacks to be so much more accessible. For instance, you may sometimes need to repeat lots of Try…Catch statements in your code, and find that hard to keep DRY. This is where using callbacks are useful, like so…

Read the rest of this entry »

Advertisements

Perfect match: Composite and Visitor Pattern


In one of my earliest post, I talked about the importantance of understanding principles of good programming, and the useful-ness of Design Patterns… in this post I intend to share some implementation of some of these patterns. Patterns are something I use from time to time on projects, and something that I’m continuously learning from reading and from others, and still enjoying it very much. Let’s proceed then…

Design Patterns can be categorized into creational, structural and behavioral. I find two patterns that work pretty well together being the Composite Pattern (structural) and the Visitor Pattern (behavior). Composite Pattern is a structural pattern that helps one create tree-like hierarchial structures whereas the Visitor Pattern is a behavioral pattern that allows a visitor object to “visit” each element in a structural hierarchy to perform some operation/behavior on that element. Read the rest of this entry »

WPF: DragDrop Decorator for ItemsControl


Drag and Drop functionality in WPF is pretty similar to how you do it in Winforms, using DataObject, DragEffects, etc in the System.Windows namespace. In Winforms, there’s a DoDragDrop method in the Control class, but in WPF, it’s been extracted out into a DragDrop static helper class. Writing drag and drop code can be quite repetitive and boring, and can be abstracted out to handle most scenarios. In this post, I have done so using the Decorator pattern.

Read the rest of this entry »

Using Factory Method Pattern with System.Activator


Over the time I spent developing, I have used Factory Method Pattern numerous times to create my objects. It provides a layer of abstraction by using an interface (or abstract base class) to hide away your concrete implementations. At runtime, we don’t care about the concrete objects that is being returned because we should always strive to program to an interface, not an implementation.
Read the rest of this entry »

WPF Commands Part 3: Custom RoutedUICommand


Welcome to Part 3 of WPF Commands. This will be the second last of the Basics for WPF Commands. For this entry, we will be looking at creating a custom RoutedUICommand, and how to make use of CommandTarget and CommandParameter properties.

First of all, you will typically need to create custom commands if the ones you get out of the box from WPF is insufficient. I would not think that this is a common scenario, but it would be good to know how to do it. If you need to do this, I recommend putting your custom controls into a separate class, similar to WPF. Let’s call our custom class CommandLibrary.

public static class CommandLibrary
{
    private static RoutedUICommand add = new RoutedUICommand("Add", "Add", typeof(CommandLibrary));

    private static RoutedUICommand remove = new RoutedUICommand("Remove", "Remove", typeof(CommandLibrary));

    public static RoutedUICommand AddListItem
    {
        get { return add; }
    }

    public static RoutedUICommand RemoveListItem
    {
        get { return remove; }
    }

}

Read the rest of this entry »

WPF Commands Part 2: Command Bindings and Gestures


Welcome to Part 2 of WPF Commands. If you have not looked at my previous entry talking about the basics, you should have a look at it here. That should set you up for understanding this example.

What are Command Bindings? Simply put it, it allows us to specify the commands that a particular control will be listening to, and the event handlers that will implement the command logic. If you remember that all commands in WPF implement ICommand interface, it defines two methods CanExecute and Execute. Controls (ones that can handle input focus) typically understand how to execute commands on themselves. So now, how do we “make” a control understand a different command?

For example, I want to be able to make a TabContainer change tabs on a button click. How do we achieve that? No brainer here, with Command Bindings. To make it more exciting, WPF also allows us to hook up commands with Input Gestures. It’s an abstract class, and we will be dealing with two types of gestures, namely KeyGesture and MouseGesture. Examples of mouse gestures are left button click, scroll wheel click and key gestures speak for themselves. Both kinds of gestures allow us to also define an optional control modifier, e.g. Control + Left Button Click, or Shift + C key. A Command Binding allows us to wire up a control to a custom Command, or WPF’s library of Commands, or Input Gestures.

The sample application will be the same as my previous post, but we will be using the Buttons and key gestures to navigate between the tabs on the Tab Container. Please take a look at this video of this demo.

wpfcommand12

Read the rest of this entry »

WPF Commands Part 1: Basics


WPF has introduced to us developers a wide new range of concepts and useful features, one of which is the Command System. It is based on the infamous Command Pattern, with a slight twist. It’s basically utilizing the Command Pattern with routing facility, as termed by Microsoft as the Routed Command System. Examining this closely, it’s similar to the Routed Event system that is inherent in WPF, and they used it for sending Commands for the UI controls to trigger a certain behaviour.

In one of my previous projects using WinForms and Model-View-Presenter (MVP) pattern, we faced the challenge of implementing a TreeView that is capable of triggering certain behaviours at different TreeNode Presenters. One of the developers came up with a very interesting implementation of using the Command Pattern and a variation of the Visitor Pattern. It was complicated, being a fresh developer, it took me hours to understand. Later he told me this pattern he used is known as the Double Dispatch pattern, which I later read about in a Design Pattern book. Now looking at Routed Command has reminded me of this, and is a similar implementation of the Double Dispatch.

It’s very interesting to see that Microsoft has started using a lot of Patterns in WPF, like Command and Observer. MS has also introduced a slight varation of the MVP, called Model-View-ViewModel pattern alongside the Pattern and Practices guide. In my opinion, this is a breath of fresh air, and .NET developers can no longer be “excused” from knowing about using and understanding patterns and how it helps to write elegant code, and decoupling your application.

Read the rest of this entry »