Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 围棋中如何正确使用构图_Go_Composition_Reusability - Fatal编程技术网

Go 围棋中如何正确使用构图

Go 围棋中如何正确使用构图,go,composition,reusability,Go,Composition,Reusability,我是新来的;有两个文件具有相似的行为,并且被告知使用组合以避免重复代码,但不能完全理解组合的概念 这两个文件具有相同的功能,但彼此之间存在差异 player1.开始 package game type confPlayer1 interface { Set(string, int) bool Move(string) bool Initialize() bool } func Play(conf confPlayer1) string { // code f

我是新来的;有两个文件具有相似的行为,并且被告知使用组合以避免重复代码,但不能完全理解组合的概念

这两个文件具有相同的功能,但彼此之间存在差异


player1.开始

package game

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

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

// ... other funcs
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
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.
}
package game

type confPlayer1 interface {
    confPlayer2
    Initialize() bool
}

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

type Player struct{
  Name string
  ...........
  ...........
}


func (p Player) Set(){
  .......
  .......
}

func (p Player) Move(){
  ........
  ........
}


func Play(confPlayer2 player){
   player.Move()
   player.Set()
}

player2.开始

package game

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

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

// ... other funcs
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
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.
}
package game

type confPlayer1 interface {
    confPlayer2
    Initialize() bool
}

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

type Player struct{
  Name string
  ...........
  ...........
}


func (p Player) Set(){
  .......
  .......
}

func (p Player) Move(){
  ........
  ........
}


func Play(confPlayer2 player){
   player.Move()
   player.Set()
}

是否有一种方法可以将所有内容合并成一个播放器。转到文件

戈朗使用构图

  • 对象组合:使用对象组合代替继承(在大多数传统语言中都使用继承)。 对象组合意味着一个对象包含另一个对象的对象 对象(比如对象X)并将对象X的职责委托给它。 这里不是重写函数(如在继承中),而是 委托给内部对象的调用
  • 接口组合:在接口组合中,接口可以组合其他接口,并在中声明所有方法集 内部接口成为该接口的一部分
  • 现在,为了具体回答您的问题,您在这里讨论的是接口组合。您还可以在此处看到代码片段:

    检查以下代码:

    player2.开始

    package game
    
    type confPlayer1 interface {
        Set(string, int) bool
        Move(string) bool
        Initialize() bool
    }
    
    func Play(conf confPlayer1) string {
        // code for Player1
    }
    
    // ... other funcs
    
    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
    
    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.
    }
    
    package game
    
    type confPlayer1 interface {
        confPlayer2
        Initialize() bool
    }
    
    func Play(conf confPlayer1) string {
        // code for Player1
    }
    
    package game
    
    type Player struct{
      Name string
      ...........
      ...........
    }
    
    
    func (p Player) Set(){
      .......
      .......
    }
    
    func (p Player) Move(){
      ........
      ........
    }
    
    
    func Play(confPlayer2 player){
       player.Move()
       player.Set()
    }
    
    player1.开始

    package game
    
    type confPlayer1 interface {
        Set(string, int) bool
        Move(string) bool
        Initialize() bool
    }
    
    func Play(conf confPlayer1) string {
        // code for Player1
    }
    
    // ... other funcs
    
    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
    
    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.
    }
    
    package game
    
    type confPlayer1 interface {
        confPlayer2
        Initialize() bool
    }
    
    func Play(conf confPlayer1) string {
        // code for Player1
    }
    
    package game
    
    type Player struct{
      Name string
      ...........
      ...........
    }
    
    
    func (p Player) Set(){
      .......
      .......
    }
    
    func (p Player) Move(){
      ........
      ........
    }
    
    
    func Play(confPlayer2 player){
       player.Move()
       player.Set()
    }
    
    在上面的代码片段中,confPlayer1接口中包含了接口confPlayer2,除了初始化函数,它只是confPlayer1的一部分

    现在,您可以将接口confPlayer2用于播放器2,将接口confPlayer1用于播放器1。请参阅下面的代码片段:

    玩家。开始

    package game
    
    type confPlayer1 interface {
        Set(string, int) bool
        Move(string) bool
        Initialize() bool
    }
    
    func Play(conf confPlayer1) string {
        // code for Player1
    }
    
    // ... other funcs
    
    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
    
    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.
    }
    
    package game
    
    type confPlayer1 interface {
        confPlayer2
        Initialize() bool
    }
    
    func Play(conf confPlayer1) string {
        // code for Player1
    }
    
    package game
    
    type Player struct{
      Name string
      ...........
      ...........
    }
    
    
    func (p Player) Set(){
      .......
      .......
    }
    
    func (p Player) Move(){
      ........
      ........
    }
    
    
    func Play(confPlayer2 player){
       player.Move()
       player.Set()
    }
    

    1.为接口指定有意义的名称,而不是
    confPlayer1
    confPlayer2
    等2。将
    confPlayer1
    拆分为一个具有
    Set+Move
    方法的界面,另一个具有
    Initialize
    组合的界面实际上与文件无关。您可以将它们合并到一个
    播放器中。只需这样做即可进入
    文件。Go对同一文件中的内容没有限制。发人深省的问题+1。这是我可以实现的。谢谢你的回答。我对物体的构成很好奇。假设player2的func Move()与player1相同,但有额外的逻辑。这是一个对象组合可以发挥作用的场景吗?是的,但对象组合的工作方式与继承不同。在对象组合中,每个对象都被分配了一个职责,并且它们在其职责范围内工作。例如:是的,但对象组合的工作方式与继承不同。在对象组合中,每个对象都被分配了一个职责,并且它们在其职责范围内工作。例如:在游戏中,玩家1有责任奔跑,玩家2有责任向阻挡玩家1的人开枪,所以,每当玩家1发现有人挡着他的路时,它就会停下来,并将发射子弹的责任委托给他,一旦子弹发射完毕,它就会再次开始运行。我喜欢你对合成的定义,其中每个物体都被分配了一个责任+1。In-Go合成看起来像继承,但它不是继承。