I'm writing a series of classes that all implement the same interface. While writing them, I've come to realise that they all use the same logic. Given the design of the interfaces, I can't think of a way to refactor the classes to use a common interface, so I was thinking Partial could work.
Interfaces:
public interface IStepStatus : INotifyPropertyChanged
{
event EventHandler StepCompleted;
event EventHandler StatusUpdate;
}
public abstract class Step1 : IStepStatus
{
//Methods unique to this step
}
public abstract class Step2 : IStepStatus
{
//Methods unique to this step
}
My implementations:
public class ImplStep1 : Step1
{
private bool Completed()
{
//Several lines of logic common across both Steps
//Trigger StepCompleted
}
private void StatusUpdate(string statusMessage)
{
//Several lines of logic common across both Steps
//Trigger StatusUpdate
}
public event EventHandler StepCompleted;
public event EventHandler StatusUpdate;
//Content unique to this class
}
public class ImplStep2 : Step2
{
//Same logic for Completed and StatusUpdate
//Content unique to this class
}
Partial
is a way to split implementation of the class in two different files - usually, when one file is generated by some tool, and another file is where you put additional stuff. It has nothing to do with what you are describing here.
The most obvious way to combine the functionality into a single place is the inheritance that you already using. For example,
public abstract class Steps : IStepStatus
{
private bool Completed()
{
//Several lines of logic common across both Steps
//Trigger StepCompleted
}
private void StatusUpdate(string statusMessage)
{
//Several lines of logic common across both Steps
//Trigger StatusUpdate
}
...
}
public class Step1 : Steps
{
//Methods unique to this step
}
public class Step2 : Steps
{
//Methods unique to this step
}
Since you are focusing your question on Partial that is in my opinion irrelevant to what you are trying to do - it is possible that I misunderstood the real problem. If so please comment or update the question.
Also, not sure from your question why your Step1
and Step2
classes are abstract
. Obviously, if they should, you can make Steps
class abstract.