Java overrides static method

I found myself in the need to override the static method, simply because it makes the most sense, but I also know that this is not possible.

Superclass, Entity.java:

abstract public class Entity<T> { public Entity() { //set up database connection } abstract public static Map<Object, T> getAll(); abstract public void insert(); abstract public void update(); protected void getData(final String query) { //get data via database } protected void executeQuery(final String query) { //execute sql query on database } } 

One of many specific implementations, Account.java:

 public class Account extends Entity<Account> { private final static String ALL_QUERY = "SELECT * FROM accounts"; private final static String INSERT_QUERY = "INSERT INTO accounts (username, password) VALUES(?, ?)"; private final static String UPDATE_QUERY = "UPDATE accounts SET password=? WHERE username=?"; private String username; private String password; public Account(final String username, final String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(final String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(final String password) { this.password = password; } @Override public static Map<Object, Account> getAll() { //return a map using the ALL_QUERY string, calls getData(string); } @Override public void insert() { //insert this using INSERT_QUERY, calls executeQuery(string); } @Override public void update() { //update this using UPDATE_QUERY, calls executeQuery(string); } } 

I did not delve into the explanation of the code, but any general feedback on this would also be appreciated, I hope the comments explain enough.

Basically, I think we can all agree that using Account.getAll() makes more sense with respect to new Account().getAll() (if I were to introduce dummy syntax for it). However, I want it to extend the Entity class, currently it is intended only for convenience, but later I may have to use Entity sets / lists / multisets and execute the update() action for all of them, for example, if I built some queue that performs all updates every minute.

So good, is there a way to build getAll() correctly?

Sincerely.

+4
source share
2 answers

You can have separate classes for operations on all elements:

 abstract public class Collection<T extends Entity<T>> { abstract public static List<T> getAll(); public void printAll() { // Print all entries of List obtained from getAll() } } 

What could you use like:

 public class Accounts extends Collection<Account> { @Override public List<Account> getAll() { //return a list using the ALL_QUERY string, calls getData(string); } } 
+1
source

It does not seem to me that this is really “simply because it makes the most sense”.

Linking perseverance in your organization is not a good idea. There are already many templates that give an appropriate design on this issue.

For example, in Domain Driven Design, “Persistence Notorance” is what people are trying to achieve. Consider creating a repository for each of your objects:

 interface Repository<T> { List<T> findAll(); void insert(T); void update(T); } 

so that you can override it in any way:

 interface UserRepository extends Repository<User> { // some other methods which is meaningful for User User findByLoginName(String loginName); } class UserRepositoryImpl implements UserRepository { List<User> findAll() { // call whatever query } void insert(T){...} void update(T){...} User findByLoginName(String loginName) {...} } 

With the proper design and component to handle searching / storing the entity, you can have an object with less stability and a repository that can perform the correct "override".

+1
source

Source: https://habr.com/ru/post/1480576/


All Articles