一、大话设计模式 之 简单工厂模式

模式特点:工厂根据条件产生不同功能的类。
程序实例:四则运算计算器,根据用户的输入产生相应的运算类,用这个运算类处理具体的运算。

package main

import (
"fmt"
)

//BaseOperation接口
type Operation interface {
getResult() float64
SetNumA(float64)
SetNumB(float64)
}

type BaseOperation struct {
numberA float64
numberB float64
}

func (operation *BaseOperation) SetNumA(numA float64) {
operation.numberA = numA
}

func (operation *BaseOperation) SetNumB(numB float64) {
operation.numberB = numB
}

type OperationAdd struct {
BaseOperation
}

func (this *OperationAdd) getResult() float64 {
return this.numberA + this.numberB
}

type OperationSub struct {
BaseOperation
}

func (this *OperationSub) getResult() float64 {
return this.numberA - this.numberB
}

type OperationMul struct {
BaseOperation
}

func (this *OperationMul) getResult() float64 {
return this.numberA * this.numberB
}

type OperationDiv struct {
BaseOperation
}

func (this *OperationDiv) getResult() float64 {
if this.numberB == 0 {
panic("被除数不能为0")
}
return this.numberA / this.numberB
}

type OperationFactory struct {
}

func (this *OperationFactory) createOperation(operator string) (operation Operation) {
switch operator {
case"+":
operation = new(OperationAdd)
case"-":
operation = new(OperationSub)
case"/":
operation = new(OperationDiv)
case"*":
operation = new(OperationMul)
default:
panic("运算符号错误!")
}
return
}

func main() {
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
var fac OperationFactory
oper := fac.createOperation("/")
oper.SetNumA(3.0)
oper.SetNumB(0.0)
fmt.Println(oper.getResult())
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值