dso89762
2018-06-15 05:37
浏览 56
已采纳

如何将此Java接口和继承结构转换为Golang? [关闭]

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?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongmei3498 2018-06-15 07:40
    已采纳

    I figured out how to achieve the same thing by myself, even though I enjoyed the "Just learn Go" answers I got.

    It's solved by creating a wrapper function userLoggedInWebEntry that takes in a WebEntryUserLoggedIn function signature, but returns a WebEntry function signature.

    It achieves the same thing as the Java inheritance code above, by letting the developer only implement a func( user User ) string and the logic in userLoggedInWebEntry is not duplicated

    https://play.golang.org/p/adrk46YFIl8

    package main
    
    import (
        "fmt"
    )
    
    type ConnectionData string
    type SessionData string
    type User string
    
    type WebEntry func(connectionData ConnectionData, sessionData SessionData) string
    type WebEntryUserLoggedIn func( user User ) string
    
    func main() {
        var webentries []WebEntry
        webentries = append( webentries, userLoggedInWebEntry(SavePasswordWebEntry) )
    
        // Test the invocation
        fmt.Println( webentries[0]("ConnectionData", "SessionData") )
    }
    
    
    func userLoggedInWebEntry( webEntry WebEntryUserLoggedIn ) WebEntry {
    
        return func(connectionData ConnectionData, sessionData SessionData) string {
            // // THIS IS THE LOGIC I DO NOT WANT TO DUPLICATE, HENCE ITS USING CLASS INHERITANCE
            var user User = "John"
            return webEntry( user )
        } 
    
    }
    
    
    func SavePasswordWebEntry( user User ) string {
        return fmt.Sprintf("user is %s", user )
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题