行为逻辑的抽象——有设计模式吗?
我需要抽象一些行为代码,并且在尝试引用调用这些行为的类中的对象时遇到问题,让我尝试解释一下:
我的“父”类有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将行为抽象到它自己的类中。然后委托给它。如果一定要命名的话,我想这就是“策略”模式。
例如:
可能最终会这样:
现在您可以在各处使用 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:
Might end up like:
Now you can use PageModifier all over the place and the behavior is kept in one location.
不了解 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.
将逻辑移至其单独的类中,公开其相关方法,并在需要时从初始类中调用它们。通常,服务方法将采用类似的参数作为初始类的实例。
虽然我觉得这里不是直接讲设计模式的,更多关于设计原理的,可以看看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.