1. 何为工厂模式
写socket开源框架时当用多协议时出现的疑问可不可以使用传统面向对象语言的工厂模式根据不同情况调用不同协议。
工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式。
在面向对象的编程语言中(如java,C++)设计模式的概念广为人知, 应用的也非常广泛。设计模式让我们的代码变得灵活起来,具有很强的扩展性。但在与C语言比肩的Go语言中,设计模式的概念并没有十分突出,甚至很少听到。在Go的开发中,借鉴design pattern的理念同样回味无穷我们的开发带来极大的便利。
2.golang中简单工厂模式
package main
import (
"fmt"
)
type Operater interface {
Operate(int, int) int
}
type AddOperate struct {
}
func (this *AddOperate) Operate(rhs int, lhs int) int {
return rhs + lhs
}
type MultipleOperate struct {
}
func (this *MultipleOperate) Operate(rhs int, lhs int) int {
return rhs * lhs
}
type OperateFactory struct {
}
func NewOperateFactory() *OperateFactory {
return &OperateFactory{}
}
func (this *OperateFactory) CreateOperate(operatename string) Operater {
switch operatename {
case "+":
return &AddOperate{}
case "*":
return &MultipleOperate{}
default:
panic("无效运算符号")
return nil
}
}
func main() {
Operator := NewOperateFactory().CreateOperate("+")
fmt.Printf("add result is %d\n", Operator.Operate(1, 2))
}
3.golang中工厂方法模式
package main
import (
"fmt"
)
type Operation struct {
a float64
b float64
}
type OperationI interface {
GetResult() float64
SetA(float64)
SetB(float64)
}
func (op *Operation) SetA(a float64) {
op.a = a
}
func (op *Operation) SetB(b float64) {
op.b = b
}
type AddOperation struct {
Operation
}
func (this *AddOperation) GetResult() float64 {
return this.a + this.b
}
type SubOperation struct {
Operation
}
func (this *SubOperation) GetResult() float64 {
return this.a - this.b
}
type MulOperation struct {
Operation
}
func (this *MulOperation) GetResult() float64 {
return this.a * this.b
}
type DivOperation struct {
Operation
}
func (this *DivOperation) GetResult() float64 {
return this.a / this.b
}
type IFactory interface {
CreateOperation() Operation
}
type AddFactory struct {
}
func (this *AddFactory) CreateOperation() OperationI {
return &(AddOperation{})
}
type SubFactory struct {
}
func (this *SubFactory) CreateOperation() OperationI {
return &(SubOperation{})
}
type MulFactory struct {
}
func (this *MulFactory) CreateOperation() OperationI {
return &(MulOperation{})
}
type DivFactory struct {
}
func (this *DivFactory) CreateOperation() OperationI {
return &(DivOperation{})
}
func main() {
fac := &(AddFactory{})
oper := fac.CreateOperation()
oper.SetA(1)
oper.SetB(2)
fmt.Println(oper.GetResult())
}
4.golang中抽象工厂模式
package main
import "fmt"
type GirlFriend struct {
nationality string
eyesColor string
language string
}
type AbstractFactory interface {
CreateMyLove() GirlFriend
}
type IndianGirlFriendFactory struct {
}
type KoreanGirlFriendFactory struct {
}
func (a IndianGirlFriendFactory) CreateMyLove() GirlFriend {
return GirlFriend{"Indian", "Black", "Hindi"}
}
func (a KoreanGirlFriendFactory) CreateMyLove() GirlFriend {
return GirlFriend{"Korean", "Brown", "Korean"}
}
func getGirlFriend(typeGf string) GirlFriend {
var gffact AbstractFactory
switch typeGf {
case "Indian":
gffact = IndianGirlFriendFactory{}
return gffact.CreateMyLove()
case "Korean":
gffact = KoreanGirlFriendFactory{}
return gffact.CreateMyLove()
}
return GirlFriend{}
}
func main() {
a := getGirlFriend("Indian")
fmt.Println(a.eyesColor)
}
主要是用来记录核心代码思想,引用地址
有疑问加站长微信联系(非本文作者)