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()
}
是否有一种方法可以将所有内容合并成一个播放器。转到文件 戈朗使用构图
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合成看起来像继承,但它不是继承。