Shall I extend the service class or just use an instance of it?

Freewind

There is a StoreService in the project, which provides some methods to read from and write to a remote data store. e.g.

public class StoreService {
    public StoreService(String url, String username, String password) {}
    public void save(String category, String key, String value) {}
    public String read(String category, String key) {}
}

Then I see there is another UserStoreService which is in a sub-module of the project, which extends StoreService:

public class UserStoreService extends StoreService {
    public UserStoreService(String url, String username, String password) {
        super(url, username, password);
    }
    public void saveUserName(String userId, String userName) {
        super.save("user", userId, userName);
    }
    public void getUserName(String userId, String userName) {
        return super.read("user", userId);
    }
}

You can see it just add some methods. Is it good to use extends here?

I can make it without extends:

public class UserStoreService {
    private StoreService storeService;
    public UserStoreService(String url, String username, String password) {
        this.storeService = new StoreService(url, username, password);
    }
    public void saveUserName(String userId, String userName) {
        storeService.save("user", userId, userName);
    }
    public void getUserName(String userId, String userName) {
        return storeService.read("user", userId);
    }
}

But I'm not sure which solution is better, and when should I use extends and when not.

creichen

This really depends on your requirements-- there is no intrinsic 'better or worse' in these scenarios.

Off the top of my head, here are some considerations:

Advantages of inheritance (first scenario):

  1. You can easily use your new subclass in place of the original one.
  2. New methods in the superclass get inherited automatically, maintaining functionality and compatibility.
  3. Method calls don't need to follow an additional pointer, so this can be slightly faster.

Advantages of delegation (second scenario):

  1. You can easily switch the delegate object as needed.
  2. New methods in the superclass don't get inherited automatically, so you'll get errors that warn you of missing functionality in the subclass.
  3. New methods in the superclass don't get inherited automatically, so you can avoid supporting functionality that you don't want to support.

For delegation in this scenario, you can also consider having a common super-interface, which will often give you the best of both worlds.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Shall I create a Superclass I will never instance just for casting different subclasses?

From Dev

Shall I use package name or class name to reprensent a concept?

From Dev

Shall I use static here or not?

From Dev

Can I just use a Generic Repository and a Generic Service class for the whole project?

From Dev

Shall I use "directive" or "controller" for this case?

From Dev

Shall I use POJO or JSONObject for REST calls

From Dev

animate menu: shall I use canvas or javascript?

From Dev

How shall I use `docker rm -v`?

From Dev

Why can't I extend the Fixnum class in a module and use it?

From Dev

Why can't I extend the Fixnum class in a module and use it?

From Dev

Android, should I use a service or just broadcast Receiver?

From Dev

How do I extend class derived from StatelessService in Service Fabric ASP.NET 5 - based service?

From Java

Shall I use WebSocket on ports other than 80?

From Dev

Which datatype shall i use to store image in database(PostgreSQL)?

From Dev

Shall I use Visual Studio Express or Visual Studio 2013?

From Dev

shall I use callback pattern in my custom function? node js

From Dev

VB.Net - when shall I use "New" word?

From Dev

Which java collection shall I use to store my data?

From Dev

Java programming to interfaces shall I use it all the time, especially for collections

From Dev

VB.Net - when shall I use "New" word?

From Dev

shall I use callback pattern in my custom function? node js

From Dev

Should I ever use @Transactional on a Service class

From Dev

Can I use a class insted of an instance of a class for better readability?

From Dev

Should I extend the system types, or just create a static utility class? How do I know when to do which?

From Dev

Create instance of all classes that extend a base class

From Dev

Is there any way to don't use 'instance of' with objects that I can't extend?

From Dev

Is there any way to don't use 'instance of' with objects that I can't extend?

From Dev

Clone a class instance, changing just a few of the properties

From Dev

Should I use the class or instance method to increment my object?

Related Related

  1. 1

    Shall I create a Superclass I will never instance just for casting different subclasses?

  2. 2

    Shall I use package name or class name to reprensent a concept?

  3. 3

    Shall I use static here or not?

  4. 4

    Can I just use a Generic Repository and a Generic Service class for the whole project?

  5. 5

    Shall I use "directive" or "controller" for this case?

  6. 6

    Shall I use POJO or JSONObject for REST calls

  7. 7

    animate menu: shall I use canvas or javascript?

  8. 8

    How shall I use `docker rm -v`?

  9. 9

    Why can't I extend the Fixnum class in a module and use it?

  10. 10

    Why can't I extend the Fixnum class in a module and use it?

  11. 11

    Android, should I use a service or just broadcast Receiver?

  12. 12

    How do I extend class derived from StatelessService in Service Fabric ASP.NET 5 - based service?

  13. 13

    Shall I use WebSocket on ports other than 80?

  14. 14

    Which datatype shall i use to store image in database(PostgreSQL)?

  15. 15

    Shall I use Visual Studio Express or Visual Studio 2013?

  16. 16

    shall I use callback pattern in my custom function? node js

  17. 17

    VB.Net - when shall I use "New" word?

  18. 18

    Which java collection shall I use to store my data?

  19. 19

    Java programming to interfaces shall I use it all the time, especially for collections

  20. 20

    VB.Net - when shall I use "New" word?

  21. 21

    shall I use callback pattern in my custom function? node js

  22. 22

    Should I ever use @Transactional on a Service class

  23. 23

    Can I use a class insted of an instance of a class for better readability?

  24. 24

    Should I extend the system types, or just create a static utility class? How do I know when to do which?

  25. 25

    Create instance of all classes that extend a base class

  26. 26

    Is there any way to don't use 'instance of' with objects that I can't extend?

  27. 27

    Is there any way to don't use 'instance of' with objects that I can't extend?

  28. 28

    Clone a class instance, changing just a few of the properties

  29. 29

    Should I use the class or instance method to increment my object?

HotTag

Archive