How can I represent state of method execution in base class without affecting the implementation in the derived classes?
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?
If I understand this correctly you want to call ReportSuccess()
if the PerformWork()
is successfully and ReportFailure()
if it fails?
Why not change
public abstract void PerformWork();
to
public void Start()
{
bool result = false;
// This will enable Perform work to operate in its own thread
Action threadAction = new Action(() =>
{
result = PerformWork();
});
ThreadStart ts = new ThreadStart(threadAction);
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.
if(result)
{
ReportSuccess();
}
else
{
ReportFailure();
}
}
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?
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...
精彩评论