Help with interface best pracice (beginner)

So i have this interface FrontEnd

package game

type FrontEnd interface {
    PresentBoard(Board)
    AwaitMove(Board) (byte, byte)
    EndGame(board Board, winner Tic, moves byte)
}

I want to have many different front-ends with these three methods. For example CLIFrontEnd. My solution for this was making an empty struct and adding the methods like this:

package cli

type cliFrontEnd struct{}

func (cliFrontEnd) AwaitMove(board game.Board) (byte, byte) {
    // ...
}

func (cliFrontEnd) PresentBoard(board game.Board) {
    // ...
}

func (cliFrontEnd) EndGame(board game.Board, winner game.Tic, moves byte) {
    // ...
}

Then a method in the cli package for creating the cli front-end:

func New() cliFrontEnd {
    return cliFrontend{}
}

Is this the best way to do this? I feel like there are better ways, I don’t see the point of the empty struct for example, and is the New method how i should do it? Seemed clean so i can type cli.New() in other packages but not sure what is the norm in go?

Add comments if anything is unclear :slight_smile:

You are using a adapter design pattern, there is no right or wrong.

I like to use interface type to solve this problem you brought up in the post.

ex here: Adapter Design Pattern in Go (GoLang) - Welcome To Golang By Example

I used this pattern on prestd here: prest/adapters at main · prest/prest · GitHub

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.