Go和Java实现简单工厂模式
2023-12-22 23:16:48
Go和Java实现简单工厂模式
本文通过计算器案例来说明简单工厂模式的使用,使用Go语言和Java语言实现。
1、简单工厂模式
简单工厂模式对对象创建管理方式最为简单,只需要创建一个简单的工厂类然后在里面创建对象,该模式通过向工
厂传递类型来指定要创建的对象。
由于 Go 本身是没有构造函数的,一般而言我们采用 NewName 的方式创建对象/接口,当它返回的是接口的时
候,其实就是简单工厂模式。
2、Go实现简单计算器
下面实现一个简单的计算器,包含简单的加减乘除功能。
package factory
// 这里实现一个简单的计算器
// 计算接口,包含计算数的设置和结果的计算
type Operation interface {
// 设置进行计算的两个数
SetNumberA(float64)
SetNumberB(float64)
// 计算
Calculate() (float64, error)
}
package factory
// ============加法===========
type AddOperation struct {
// 包含两个操作数
numberA float64
numberB float64
}
func (addOperation *AddOperation) Calculate() (float64, error) {
return addOperation.numberA + addOperation.numberB, nil
}
func (addOperation *AddOperation) SetNumberA(numberA float64) {
addOperation.numberA = numberA
}
func (addOperation *AddOperation) SetNumberB(numberB float64) {
addOperation.numberB = numberB
}
package factory
// ============减法============
type SubOperation struct {
// 包含两个操作数
numberA float64
numberB float64
}
func (subOperation *SubOperation) Calculate() (float64, error) {
return subOperation.numberA - subOperation.numberB, nil
}
func (subOperation *SubOperation) SetNumberA(numberA float64) {
subOperation.numberA = numberA
}
func (subOperation *SubOperation) SetNumberB(numberB float64) {
subOperation.numberB = numberB
}
package factory
// ============乘法============
type MulOperation struct {
// 包含两个操作数
numberA float64
numberB float64
}
func (mulOperation *MulOperation) Calculate() (float64, error) {
return mulOperation.numberA * mulOperation.numberB, nil
}
func (mulOperation *MulOperation) SetNumberA(numberA float64) {
mulOperation.numberA = numberA
}
func (mulOperation *MulOperation) SetNumberB(numberB float64) {
mulOperation.numberB = numberB
}
package factory
import "errors"
// ============除法============
type DivOperation struct {
// 包含两个操作数
numberA float64
numberB float64
}
func (divOperation *DivOperation) Calculate() (float64, error) {
if divOperation.numberB == 0 {
return -1, errors.New("被除数不能为0")
} else {
return divOperation.numberA / divOperation.numberB, nil
}
}
func (divOperation *DivOperation) SetNumberA(numberA float64) {
divOperation.numberA = numberA
}
func (divOperation *DivOperation) SetNumberB(numberB float64) {
divOperation.numberB = numberB
}
package constant
const (
Add = "+"
Sub = "-"
Mul = "*"
Div = "/"
)
package factory
import "proj/constant"
// ============工厂类============
type OperationFactory struct {
}
func (operationFactory *OperationFactory) CreateOperate(operate string) Operation {
switch operate {
case constant.Add:
return &AddOperation{}
case constant.Sub:
return &SubOperation{}
case constant.Mul:
return &MulOperation{}
case constant.Div:
return &DivOperation{}
}
return nil
}
package main
import "fmt"
import . "proj/factory"
import "proj/constant"
// 测试
func main() {
// 全局变量
var numberA float64 = 200
var numberB float64 = 100
var operation Operation
var result float64
var err error
// ============创建工厂============
operationFactory := OperationFactory{}
// ============加法============
operation = operationFactory.CreateOperate(constant.Add)
operation.SetNumberA(numberA)
operation.SetNumberB(numberB)
result, err = operation.Calculate()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Add Result:", result)
}
// ============减法============
operation = operationFactory.CreateOperate(constant.Sub)
operation.SetNumberA(numberA)
operation.SetNumberB(numberB)
result, err = operation.Calculate()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Sub Result:", result)
}
// ============乘法============
operation = operationFactory.CreateOperate(constant.Mul)
operation.SetNumberA(numberA)
operation.SetNumberB(numberB)
result, err = operation.Calculate()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Mul Result:", result)
}
// ============除法============
operation = operationFactory.CreateOperate(constant.Div)
operation.SetNumberA(numberA)
operation.SetNumberB(numberB)
result, err = operation.Calculate()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Div Result:", result)
}
}
# 程序输出
Add Result: 300
Sub Result: 100
Mul Result: 20000
Div Result: 2
3、Java实现简单计算器
package com.factory;
// 这里实现一个简单的计算器
// 计算接口,包含计算数的设置和结果的计算
public abstract class Operation {
private double numberA;
private double numberB;
// 设置进行计算的两个数
public double getNumberA() {
return numberA;
}
public void setNumberA(double numberA) {
this.numberA = numberA;
}
public double getNumberB() {
return numberB;
}
public void setNumberB(double numberB) {
this.numberB = numberB;
}
// 计算
public abstract double Calculate();
}
package com.factory;
// ============加法===========
public class AddOperation extends Operation {
@Override
public double Calculate() {
return this.getNumberA() + this.getNumberB();
}
}
package com.factory;
// ============减法============
public class SubOperation extends Operation {
@Override
public double Calculate() {
return this.getNumberA() - this.getNumberB();
}
}
package com.factory;
// ============乘法============
public class MulOperation extends Operation {
@Override
public double Calculate() {
return this.getNumberA() * this.getNumberB();
}
}
package com.factory;
// ============除法============
public class DivOperation extends Operation {
@Override
public double Calculate() {
if (this.getNumberB() == 0) {
throw new RuntimeException("被除数不能为0!");
} else {
return this.getNumberA() / this.getNumberB();
}
}
}
package com.factory;
public enum OperationType {
ADD, SUB, MUL, DIV;
}
package com.factory;
// ============工厂类============
public class OperationFactory {
public Operation CreateOperate(OperationType operate) {
switch (operate) {
case ADD:
return new AddOperation();
case SUB:
return new SubOperation();
case MUL:
return new MulOperation();
case DIV:
return new DivOperation();
}
return null;
}
}
package com.factory;
public class Test {
public static void main(String[] args) {
// 全局变量
Operation operation = null;
double result = 0;
// ============创建工厂============
OperationFactory operationFactory = new OperationFactory();
// ============加法============
operation = operationFactory.CreateOperate(OperationType.ADD);
operation.setNumberA(200);
operation.setNumberB(100);
result = operation.Calculate();
System.out.println("result :" + result);
// ============减法============
operation = operationFactory.CreateOperate(OperationType.SUB);
operation.setNumberA(200);
operation.setNumberB(100);
result = operation.Calculate();
System.out.println("result :" + result);
// ============乘法============
operation = operationFactory.CreateOperate(OperationType.MUL);
operation.setNumberA(200);
operation.setNumberB(100);
result = operation.Calculate();
System.out.println("result :" + result);
// ============除法============
operation = operationFactory.CreateOperate(OperationType.DIV);
operation.setNumberA(200);
operation.setNumberB(100);
result = operation.Calculate();
System.out.println("result :" + result);
}
}
# 程序输出
result :300.0
result :100.0
result :20000.0
result :2.0
文章来源:https://blog.csdn.net/qq_30614345/article/details/131626024
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!