views:

80

answers:

3

Hi all,

I need to abstract some behavioural code and have a problem trying to reference the objects in the class that is calling these behaviours, let me try to explain:

My "parent" class, has a property called CurrentPage. I also have some behavioural logic, that modifies the CurrentPage property, currently this is written in the same class. I now need to reuse that behaviour in lots of places so I want to encapsulate/abstract that into a seperate ... erm... class??

I can sense that there is probably a design pattern that will meet my needs, but I can't figure out which one to use.

Can anyone out there help??

Thanks, Mark

(I am using C#, Silverlight and MVVM. CurrentPage is a notification property, not a field, so cannot be passed as a ref type into a Behaviour sub-class)

UPDATE: Example added as requested:

class MainApp
{
    public static string CurrentPage { get; set; }

    /// <summary>
    /// Entry point into console application.
    /// </summary>
    static void Main()
    {
        CurrentPage = "Default value";

        Console.WriteLine(CurrentPage);

        DoWork();

        Console.WriteLine(CurrentPage);

        Console.ReadLine();

    }


    private static void DoWork()
    {
        CurrentPage = "A new page";
    }

}

I'm trying to extract DoWork() into a seperate class.

+3  A: 

Abstract the behavior into its own class. Then delegate to it. If you had to name it, I guess this is the "strategy" pattern.

For example:

class MainApp
{
    ...

    void DoWork()
    {
        CurrentPage = "A new page";
    }
}

Might end up like:

class PageModifier
{
    void ModifyCurrentPage(MainApp instance)
    {
        instance.CurrentPage = "A new page";
    }
}

class MainApp
{
    ...
    PageModifier _mod;

    void DoWork()
    {
        _mod.ModifyCurrentPage(this);
    }
}

Now you can use PageModifier all over the place and the behavior is kept in one location.

TheDeeno
Thanks for the updated. This requires and instance of MainApp as a parameter in the ModifyCurrentPage method. What If I wanted to trigger this behaviour from another class - would you suggest a polymorphic ModifyCurrentPage method, or a super class from which MainApp inherits?
Mark Cooper
It depends on what is actually needed to perform the "ModifyCurrentPage" work. If you plan on using this against multiple class instances then yes, you want to use a polymorphic method. I'd suggest using an interface to get polymorphism before creating a superclass. On the other hand, if you don't really need all the context "MainApp" supplies in this case, you likely don't need an instance of it to perform the work. If this is true, pass the needed info as parameters and return the "ModifiedPage" from the method. If If requested, I'll include this scenario in my answer.
TheDeeno
+1  A: 

Don't know c# but this sounds like it may be appropriate for an interface definition. However usually this means leaving out the actual implementation of the behaviors you wish to define.

ennuikiller
+1  A: 

Move the logic in its separate class, expose its relevant methods and call them whenever needed from your initial class. Typical, the service methods will take like argument an instance of your intial class. Although I do not feel it is directly about design patterns here, more about design principles, have a look at the GoF book; it will worth it.

Radu