Posted by: Cirilo Meggiolaro | 03/30/2009

Tip of the day #167 – Freezable objects

Available under the base elements of Windows Presentation Foundation (WPF), the freezable objects keep two different states: frozen and unfrozen. When the object state is set to frozen it becomes read-only so you cannot modify them. On the other hand, an unfrozen object you may modify it. An event called Changed is responsible for notify changes to its observers.

The main purpose of keeping those two states is to improve the performance of the objects making them frozen when no change is necessary and to remove the overhead of listening and notifying the observers every time something changes on the object.

How to…

A quick example on how to create your own freezable class:

  • Create a class that inherits from the System.Windows.Freezable abstract class (available in the WindowsBase.dll assembly);
  • The Freezable class was designed focused on dependency properties. You can use regular properties but it makes the work a little bit harder once you have to override some methods;
  • Implement the abstract method CreateInstanceCore returning a new instance of your freezable class;

To consume your freezable class:

  • Create a regular instance of your class;
  • To freeze it, just call the Freeze() method;
  • To unfreeze your class, you must create a clone by calling the Clone() method and define the properties you need to set.

The following code snippet shows a simple class that has two fields exposed by dependency properties:

public class MyFreezeClass : Freezable
{
    public string Description
    {
        get { return (string)GetValue(DescriptionProperty); }
        set { SetValue(DescriptionProperty, value); }
    }

    public static readonly DependencyProperty DescriptionProperty =
    DependencyProperty.Register(“Description”, typeof(string), typeof(MyFreezeClass));

    public bool Active
    {
        get { return (bool)GetValue(ActiveProperty); }
        set { SetValue(ActiveProperty, value); }
    }

    public static readonly DependencyProperty ActiveProperty =
    DependencyProperty.Register(“Active”, typeof(bool), typeof(MyFreezeClass));

    public MyFreezeClass(string description, bool active)
    {
        this.Description = description;
        this.Active = active;
    }

    protected override Freezable CreateInstanceCore()
    {
        return new MyFreezeClass(this.Description, this.Active);
    }
}

[…]

MyFreezeClass myFreeze = new MyFreezeClass(“A freezable class”, true);

Console.WriteLine(myFreeze.IsFrozen.ToString()); // Output: false

if (myFreeze.CanFreeze)
    myFreeze.Freeze();

Console.WriteLine(myFreeze.IsFrozen.ToString()); // Output: true

myFreeze.Active = false; // Throws an exception

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: