dongrao1862 2018-12-17 04:24
浏览 11
已采纳

如何在Go中正确使用合成

I'm new on go; have two files that share similar behavior and was told to use composition to avoid having duplicated code, but can't quite understand the concept of composition.

Both files have common functionality but have their differences one of another.


player1.go

package game

type confPlayer1 interface {
    Set(string, int) bool
    Move(string) bool
    Initialize() bool
}

func Play(conf confPlayer1) string {
    // code for Player1
}

// ... other funcs

player2.go

package game

type confPlayer2 interface {
    Set(string, int) bool
    Move(string) bool
    // Initializer is only for Player1
}

func Play(conf confPlayer2) string {
    // code for Player2, not the same as Player1.
}

// ... the same other funcs from player1.go file
// ... they differ slighly from player1.go funcs

Is there a way to combine all into a single player.go file?

  • 写回答

1条回答 默认 最新

  • dongyanzhui0524 2018-12-17 05:51
    关注

    Golang uses composition.

    1. Object composition: Object composition is used instead of inheritance (which is used in most of the traditional languages). Object composition means that an object contains object of another object (say Object X) and delegate responsibilities of object X to it. Here instead of overriding functions (as in inheritance), function calls delegated to internal objects.
    2. Interface composition: In interface composition and interface can compose other interface and have all set of methods declared in internal interface becomes part of that interface.

    Now to specifically answer your question, you are talking about interface composition here. You can also see code snippet here: https://play.golang.org/p/fn_mXP6XxmS

    Check below code:

    player2.go

    package game
    
    type confPlayer2 interface {
        Set(string, int) bool
        Move(string) bool
        }
    
    func Play(conf confPlayer2) string {
        // code for Player2, not the same as Player1.
    }
    

    player1.go

    package game
    
    type confPlayer1 interface {
        confPlayer2
        Initialize() bool
    }
    
    func Play(conf confPlayer1) string {
        // code for Player1
    }
    

    In above code snippet confPlayer1 interface has composed interface confPlayer2 in it, except Initialize function which is only part of confPlayer1.

    Now you can use interface confPlayer2 for player 2 and confPlayer1 for player1. see code snippet below:

    player.go

    package game
    
    type Player struct{
      Name string
      ...........
      ...........
    }
    
    
    func (p Player) Set(){
      .......
      .......
    }
    
    func (p Player) Move(){
      ........
      ........
    }
    
    
    func Play(confPlayer2 player){
       player.Move()
       player.Set()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码