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?