行为逻辑的抽象——有设计模式吗?

发布于 2024-08-11 01:16:46 字数 944 浏览 9 评论 0原文

我需要抽象一些行为代码,并且在尝试引用调用这些行为的类中的对象时遇到问题,让我尝试解释一下:

我的“父”类有一个名为 CurrentPage 的属性。我还有一些行为逻辑,可以修改 CurrentPage 属性,目前这是在同一个类中编写的。我现在需要在很多地方重用该行为,所以我想将其封装/抽象到一个单独的...呃...类中?

我感觉到可能有一种设计模式可以满足我的需求,但我不知道该使用哪一种。

有人可以帮忙吗?

谢谢, 标记

(我使用的是 C#、Silverlight 和 MVVM。CurrentPage 是通知属性,而不是字段,因此不能作为 ref 类型传递到行为子类中)

更新:按要求添加的示例:

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";
    }

}

我正在尝试将 DoWork() 提取到一个单独的类中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幻想少年梦 2024-08-18 01:16:46

将行为抽象到它自己的类中。然后委托给它。如果一定要命名的话,我想这就是“策略”模式。

例如:

class MainApp
{
    ...

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

可能最终会这样:

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

class MainApp
{
    ...
    PageModifier _mod;

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

现在您可以在各处使用 PageModifier,并且行为保留在一个位置。

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.

乜一 2024-08-18 01:16:46

不了解 C#,但这听起来可能适合接口定义。然而,通常这意味着忽略您希望定义的行为的实际实现。

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.

听你说爱我 2024-08-18 01:16:46

将逻辑移至其单独的类中,公开其相关方法,并在需要时从初始类中调用它们。通常,服务方法将采用类似的参数作为初始类的实例。
虽然我觉得这里不是直接讲设计模式的,更多关于设计原理的,可以看看GoF的书;这是值得的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文