1. 首页
  2. 文章

Golang设计模式——工厂模式

筑梦攻城狮 ·


package fatory
import "fmt"
type Device interface{
  StartDevice()
}
type KeyBoard struct {
}
func (k *KeyBoard) StartDevice(){
  fmt.Println("Keyboard is ready to work!")
}
type Mouse struct {
}
func (m *Mouse) StartDevice(){
  fmt.Println("Mouse is ready to work!")
}
func NewDevice(device string)Device{
  switch device{
  case "k":
   return &KeyBoard{}
  case "m":
   return &Mouse{}
  default:
   return nil
  }
}


测试用例

package fatory
import "testing"
func TestNewDevice(t *testing.T) {
  NewDevice("k").StartDevice()
  NewDevice("m").StartDevice()
}


本文来自:51CTO博客

感谢作者:筑梦攻城狮

查看原文:Golang设计模式——工厂模式

没有任何文字说明~
#1

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中