user584018
user584018

Reputation: 11354

Can I eliminate duplicate code for below derived classes and move to abstract base class

I have a abstract base class, starting a timer which is common to all derived class,

public abstract class BaseClass
{
    public virtual void Start() { _timer.Start(); }
}

Now I need to load different JSON configuration files for each derived class and create the file,

public class DerivedClass1 : BaseClass
{
    private readonly List<config> configs = new List<config>();

    public DerivedClass1()
    {
        configs = JsonSettings.GetConfigurations(@"./Configurations/1.json");
    }
    public override void Start()
    {
        base.Start();

        foreach (var configuration in configs)
        {
            JsonSettings.CreateConfigFile(configuration);
        }
    }
}

public class DerivedClass2 : BaseClass
{
    private readonly List<config> configs = new List<config>();

    public DerivedClass2()
    {
        configs = JsonSettings.GetConfigurations(@"./Configurations/2.json");
    }
    public override void Start()
    {
        base.Start();

        foreach (var configuration in configs)
        {
            JsonSettings.CreateConfigFile(configuration);
        }
    }
}

As I see there are lots of codes are duplicated in various derived class.

Can I move these piece of code as well as abstract base class or is there another way?

Upvotes: 0

Views: 68

Answers (3)

Naruto
Naruto

Reputation: 708

public interface BaseClass
{
    void Start();
}

public interface IBaseClassUtil
{
    void Start();
    void setConfigs(List<config> configs);
}

public class BaseClassUtil : IBaseClassUtil
{
    System.Timers.Timer _timer;
    public  List<config> _configs { get; set; } = new List<config>();
    public void Start()
    {
        _timer.Start();
        foreach (var configuration in _configs)
        {
            JsonSettings.CreateConfigFile(configuration);
        }
    }

    public void setConfigs(List<config> configs)
    {
        _configs = configs;
    }
}
public class DerivedClass1 : BaseClass
{
    private IBaseClassUtil _baseUtility;
    public DerivedClass1(IBaseClassUtil baseUtility)
    {
        _baseUtility = baseUtility;
        _baseUtility.setConfigs( JsonSettings.GetConfigurations(@"./Configurations/1.json"));
    }

    public void Start()
    {
        _baseUtility.Start();
    }
}

public class DerivedClass2 : BaseClass
{
    private IBaseClassUtil _baseUtility;
    public DerivedClass2(IBaseClassUtil baseUtility)
    {
        _baseUtility = baseUtility;
        _baseUtility.setConfigs(JsonSettings.GetConfigurations(@"./Configurations/2.json"));
    }

    public void Start()
    {
        _baseUtility.Start();
    }
}

This might be oveer engineered. Or might not suit ur current requirement. Advantages would be

  1. In future if you want u want to have different implementation for IBaseClassUtil it will be easier

    1. And huge advantage would be this code is testable

Upvotes: 2

V0ldek
V0ldek

Reputation: 10593

If the classes differ by nothing but the configuration path, then you can have only one derived class that takes the path as a parameter in its ctor.

public DerivedClass(string configurationPath)
{
    configs = JsonSettings.GetConfigurations(configurationPath);
}

Put please note that a decision on including inheritance in your architecture is not about code duplication, and by not giving us any information on the functions or even names of the classes (BaseClass and DerivedClass mean nothing. What do they represent? What's their function? Why are they related?) you give us no way of really helping you with your design.

Upvotes: 1

Michał Turczyn
Michał Turczyn

Reputation: 37440

I think you could simplify your code to this:

public abstract class BaseClass
{
    protected virtual List<config> configs { get; set; } = new List<config>();
    public virtual void Start()
    {
        _timer.Start();
        foreach (var configuration in configs)
        {
            JsonSettings.CreateConfigFile(configuration);
        }
    }
}

public class DerivedClass1 : BaseClass
{
    public DerivedClass1()
    {
        configs = JsonSettings.GetConfigurations(@"./Configurations/1.json");
    }
}

public class DerivedClass2 : BaseClass
{
    public DerivedClass2()
    {
        configs = JsonSettings.GetConfigurations(@"./Configurations/2.json");
    }
}

Upvotes: 3

Related Questions