3

golang中的工厂模式

 2 years ago
source link: https://cloudsjhan.github.io/2018/08/27/golang%E4%B8%AD%E7%9A%84%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F-md/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

golang中的工厂模式

发表于 2018-08-27

| 分类于 golang

| 阅读次数:

| 字数统计: 570

|

阅读时长 ≈ 3

* 简单工厂模式

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))
}
* 工厂方法
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())
}
* 抽象工厂方法
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)
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK