I have a small program.

interface Store<T extends Base> { void add(T t); Simple<T> getStore(); boolean delete(T t); boolean update(T oldObj, T newObj); } class UserStore implements Store<User> { private Simple<User> users = new SimpleArray<>(); @Override public void add(User user) { users.add(user); } public Simple<User> getStore() { return users; } @Override public boolean delete(User user) { for (int i = 0; i < users.getSize(); i++) { if (user.getId().equals(users.get(i).getId())) { users.delete(i); return true; } } return false; } @Override public boolean update(User oldObj, User newObj) { for (int i = 0; i < users.getSize(); i++) { if (oldObj.getId().equals(users.get(i).getId())) { users.update(i, newObj); return true; } } return false; } } class RoleStore implements Store<Role> { private Simple<Role> roles = new SimpleArray<>(); @Override public void add(Role role) { roles.add(role); } public Simple<Role> getStore() { return roles; } @Override public boolean delete(Role user) { for (int i = 0; i < roles.getSize(); i++) { if (user.getId().equals(roles.get(i).getId())) { roles.delete(i); return true; } } return false; } @Override public boolean update(Role oldObj, Role newObj) { for (int i = 0; i < roles.getSize(); i++) { if (oldObj.getId().equals(roles.get(i).getId())) { roles.update(i, newObj); return true; } } return false; } } И сам контент для сторов: public abstract class Base { private final String id; Base(String id) { this.id = id; } public String getId() { return id; } } public class Role extends Base { public Role(String id) { super(id); } } public class User extends Base { public User(String id) { super(id); } } 

The problem is that in the UserStore and RoleStore lot of duplicate code I want to get rid of it with the help of generics, but not how it does not work. But the main thing I want to do is so that I still have the interface Store<T extends Base> . But I cannot write an abstract class UserStore implements Store<T extends Base> {} . And I want to make a general class that instead of specific implementations of User and Role uses <T extends Base> and could work with everyone. Get flexible api and do not duplicate the code but it does not work. Help me please.

  • one
    Make the default implementation in the interface - GenCloud
  • I can't get the users variable in the fields in the methods used. And in the interface it can only be static but this is not it ... although the idea is interesting ... - Pavel

1 answer 1

You need to create an abstract class with generics and partial implementation:

 public interface Base; public class User implements Base {}; public class Role implements Base {}; public abstract class GenericStore <T extends Base> implements Store <T> { private Simple <T> items = new Simple <T> (); public void add(T item) { items.add(item); } } 

And then create it a la:

 Store<User> UserStore = new GenericStore<User>(): UserStore.add(new User()); 

So you do not need to inherit from the Store if you do not want to duplicate the operation code, but still you have to if some will have their own functional, different from the abstract class.

  • Yes! I think so too! But in my task, the signature of this interface is declaratively set, and its presence is here. And so I agree with you on all 200%. - Pavel
  • one
    Well, what's the deal then? Change the name of my class to GenericStore, inherit it from the Store and create it like this: Store <User> UsersStore = new GenericStore <User> (); Pattern Factory in its purest form. - Daniel Protopopov
  • The problem with generics I can not write public abstract class GenericStore implements Store <T extands Base> {} and use T, it makes me write either Store <User> or Store <Role> - Pavel
  • one
    If User and Role extend from Base then I don’t see why I don’t do so. Then you can write like this: public abstract class GenericStore <T extends Base> implements Store <T>. Sample answer in stackoverflow.com/questions/9893481/… - Daniel Protopopov