如何在不影响派生类中的实现的情况下表示基类中方法执行的状态?
我正在尝试创建一个抽象类,定义一个抽象方法,该方法是从在抽象方法中实现某些特定行为的其他类派生的。我希望抽象类包含某种状态信息,该信息表示派生类中的实现没有错误地退出,但我想在 AbstractClass 中而不是在派生类中实现所有状态处理。我想让派生类完全不知道 AbstractClass 中的功能。下面是一个例子。我在代码中做了注释来描述我想要实现的目标。
public abstract class AbstractClass
{
public void Start()
{
ThreadStart ts = new ThreadStart(PerformWork);
Thread t = new Thread(ts);
t.Start();
Thread.Sleep(2000);
// Dependent on if ReportWork exited without expcetions
// I want to call ReportSuccess or ReportFailure from this method.
// However, I dont want to implement any reporting functionallity (or
// as little as possible)
// into the deriving classes PerformWork() method. Simply put
// I want the deriving classes to be totally unaware of the reporting.
}
public void ReportSuccess()
{
Console.WriteLine("Success!");
}
public void ReportFailure()
{
Console.WriteLine("Failure!");
}
public abstract void PerformWork();
}
从 AbstractClass 派生的类:
class ConcreteImplementationClass:AbstractClass
{
public override void PerformWork()
{
// Implements some functionality
// without knowing anything about
// whats going on in AbstractClass.
}
}
有人对我如何实现此功能或如何创建类似的功能有任何建议吗?
I'm trying to create an abstract class, defining an abstract method, that is to be derived from other classes that implements some specific behavior in the abstract method. I want the abstract class to contain some kind of state information that represents however the implementation in the derived classes exited without errors, but I want to implement all state handling in AbstractClass and not in the deriving classes. I want to make the deriving classes totally unaware of the functionality in AbstractClass. Below is an example. I made comments in the code to describe what I'm trying to achieve.
public abstract class AbstractClass
{
public void Start()
{
ThreadStart ts = new ThreadStart(PerformWork);
Thread t = new Thread(ts);
t.Start();
Thread.Sleep(2000);
// Dependent on if ReportWork exited without expcetions
// I want to call ReportSuccess or ReportFailure from this method.
// However, I dont want to implement any reporting functionallity (or
// as little as possible)
// into the deriving classes PerformWork() method. Simply put
// I want the deriving classes to be totally unaware of the reporting.
}
public void ReportSuccess()
{
Console.WriteLine("Success!");
}
public void ReportFailure()
{
Console.WriteLine("Failure!");
}
public abstract void PerformWork();
}
A class deriving from AbstractClass:
class ConcreteImplementationClass:AbstractClass
{
public override void PerformWork()
{
// Implements some functionality
// without knowing anything about
// whats going on in AbstractClass.
}
}
Do anyone have any advice for how I could achieve this functionality, or how I could create something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解正确的话,如果
PerformWork()
成功则调用ReportSuccess()
,如果失败则调用ReportFailure()
?为什么不
改成
If I understand this correctly you want to call
ReportSuccess()
if thePerformWork()
is successfully andReportFailure()
if it fails?Why not change
to
考虑到您想在其中运行代码,然后在所有派生方法中调用 base.PerformWork() ,您是否不能将基方法设为虚拟?
Can you not make your base method virtual, given that you want to run code in it, then call base.PerformWork() in all the derived methods?
您正在描述面向方面的编程,AOP。
您可以使用任意数量的拦截策略轻松实现此目的。最简单的可能是城堡代理。
请参阅这个...
You are describing Aspect Oriented Programming, AOP.
You can easily achieve this using any number of intercept strategies. The easiest would probably be castle proxy.
See this...