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条)

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题