Is it possible to rewrite this Java structure that uses interfaces and inheritance into an idiomatic Golang way?
It's not super complicated Java code, but it shows the strength of class inheritance, but I would like to try and achieve the same result in Go somehow
The Java Code:
First there is a class Interface.
public interface WebEntry {
String perform(ConnectionData connectionData, SessionData sessionData) throws Exception;
}
Somewhere there is a list of web entries WebEntry
:
List<WebEntry> webEntries = new ArrayList<>();
webEntries.add( new SavePasswordWebEntry() );
An abstract class UserLoggedInWebEntry
that implements a WebEntry
public abstract class UserLoggedInWebEntry implements WebEntry {
@Override
public String perform(ConnectionData connectionData, SessionData sessionData) throws Exception {
// THIS IS THE LOGIC I DO NOT WANT TO DUPLICATE, HENCE ITS USING CLASS INHERITANCE
...
return userPerform(User user);
}
protected abstract String userPerform(User user) throws Exception;
}
By using class inheritance, we can create a UserLoggedInWebEntry
which still behaves as a WebEntry
but we don't need to duplicate the ...
logic each time we want a "user logged in web entry" instance:
public class SavePasswordWebEntry extends UserLoggedInWebEntry {
@Override
protected String userPerform(User user) throws Exception {
...
}
}
So the whole point is that, since its using class inheritance, a developer only needs to implement String userPerform(User user)
without duplicating the logic of ...
in UserLoggedInWebEntry
.
Is this even possible to achieve in Golang? If so, how would it look like?