mardi 1 novembre 2016

Extending base class vs. instantiating base class

I'm wondering if it's better to extend my base class some times or if I should just instantiate an object of my base class.

Option 1:

public abstract class BaseService
{
    public void DoSomething()
    {
        ...
    }
}

public class UserService : BaseService
{
}

public class MyApp
{
    public void Run()
    {
        UserService userService = new UserService();
        userService.DoSomething();
    }
}

Option 2:

public class BaseService
{
    public void DoSomething()
    {
        ...
    }
}

public class MyApp
{
    public void Run()
    {
        BaseService userService = new BaseService ();
        userService.DoSomething();
    }
}

Which option should be preferred? Option 1 has the advantage that the UserService can extend the BaseService with "user specific" methods, also my base class could have abstract methods - let's assume this is not needed (at the moment).

I'm sure this topic has been discussed before but seems like I'm searching with the wrong keywords.

Aucun commentaire:

Enregistrer un commentaire