可能会写成这样:
/// w ##class(PHA.YX.Design.Program).GetResult()
ClassMethod GetResult()
{
READ "输入第一个数字:", numberA,!
READ "输入操作符:", operate,!
READ "输入第二个数字:", numberB,!
s result = ""
i operate = "+" d
.s result = numberA + numberB
i operate = "-" d
.s result = numberA - numberB
i operate = "*" d
.s result = numberA * numberB
i operate = "/" d
.s result = numberA / numberB
q result
}
缺点:
DHC-APP> w ##class(PHA.YX.Design.Program).GetResult()
输入第一个数字:1
输入操作符:/
输入第二个数字:1.5
.6666666666666666667
DHC-APP> w ##class(PHA.YX.Design.Program).GetResult()
输入第一个数字:1
输入操作符:/
输入第二个数字:0
.s result = numberA / numberB
^
zGetResult+12^PHA.YX.Design.Program.1
可能会写成这样:
/// w ##class(PHA.YX.Design.Program).GetResultCase()
ClassMethod GetResultCase()
{
READ "输入第一个数字:", numberA,!
READ "输入操作符:", operate,!
READ "输入第二个数字:", numberB,!
s result= $case(operate,
"+" : numberA + numberB,
"-" : numberA + numberB,
"*" : numberA + numberB,
"/" : numberA + numberB)
q result
}
缺点:
Class PHA.YX.Design.Program Extends %RegisteredObject
{
/// w ##class(PHA.YX.Design.Program).SimpleFactory()
ClassMethod SimpleFactory()
{
READ "输入第一个数字:", numberA,!
READ "输入操作符:", operate,!
READ "输入第二个数字:", numberB,!
# dim oper as PHA.YX.Design.SimpleFactory.Operation
s oper = ##class(PHA.YX.Design.SimpleFactory.OperationFactory).CreateOperate(operate)
d oper.numberASet(numberA)
d oper.numberBSet(numberB)
s result = oper.GetResult()
q result
}
}
DHC-APP>w ##class(PHA.YX.Design.Program).SimpleFactory()
输入第一个数字:1
输入操作符:+
输入第二个数字:1
2
DHC-APP>w ##class(PHA.YX.Design.Program).SimpleFactory()
输入第一个数字:2
输入操作符:-
输入第二个数字:3
-1
DHC-APP>w ##class(PHA.YX.Design.Program).SimpleFactory()
输入第一个数字:6
输入操作符:*
输入第二个数字:3
18
DHC-APP>w ##class(PHA.YX.Design.Program).SimpleFactory()
输入第一个数字:1
输入操作符:/
输入第二个数字:1.5
0.6666666666666666667
DHC-APP>w ##class(PHA.YX.Design.Program).SimpleFactory()
输入第一个数字:1
输入操作符:/
输入第二个数字:0
除数不能为0。
DHC-APP>w ##class(PHA.YX.Design.Program).SimpleFactory()
输入第一个数字:3
输入操作符:**
输入第二个数字:3
27
Class PHA.YX.Design.SimpleFactory.OperationFactory Extends %RegisteredObject
{
ClassMethod CreateOperate(operate As %String) As Operation
{
# dim oper as PHA.YX.Design.SimpleFactory.Operation
s oper = $case(operate,
"+" : ##class(PHA.YX.Design.SimpleFactory.OperationAdd).%New(),
"-" : ##class(PHA.YX.Design.SimpleFactory.OperationSub).%New(),
"*" : ##class(PHA.YX.Design.SimpleFactory.OperationMul).%New(),
"/" : ##class(PHA.YX.Design.SimpleFactory.OperationDiv).%New(),
"**" : ##class(PHA.YX.Design.SimpleFactory.OperationIndex).%New())
q oper
}
}
Class PHA.YX.Design.SimpleFactory.Operation Extends %RegisteredObject
{
Property numberA As %Decimal [ InitialExpression = 0, Private ];
Method numberAGet() As %String [ ServerOnly = 1 ]
{
Quit i%numberA
}
Method numberASet(Arg As %String) As %Status [ ServerOnly = 1 ]
{
s i%numberA = Arg
Quit $$$OK
}
Property numberB As %Decimal [ InitialExpression = 0, Private ];
Method numberBGet() As %String [ ServerOnly = 1 ]
{
Quit i%numberB
}
Method numberBSet(Arg As %String) As %Status [ ServerOnly = 1 ]
{
s i%numberB = Arg
Quit $$$OK
}
Method GetResult() [ Abstract ]
{
s result = 0
q result
}
}
Class PHA.YX.Design.SimpleFactory.OperationAdd Extends PHA.YX.Design.SimpleFactory.Operation
{
Method GetResult()
{
s reulst = 0
s reulst = ..numberAGet() + ..numberBGet()
q reulst
}
}
Class PHA.YX.Design.SimpleFactory.OperationSub Extends PHA.YX.Design.SimpleFactory.Operation
{
Method GetResult()
{
s reulst = 0
s reulst = ..numberAGet() - ..numberBGet()
q reulst
}
}
Class PHA.YX.Design.SimpleFactory.OperationMul Extends PHA.YX.Design.SimpleFactory.Operation
{
Method GetResult()
{
s reulst = 0
s reulst = ..numberAGet() * ..numberBGet()
q reulst
}
}
Class PHA.YX.Design.SimpleFactory.OperationDiv Extends PHA.YX.Design.SimpleFactory.Operation
{
Method GetResult()
{
s reulst = 0
q:..numberBGet()=0 "除数不能为0。"
s reulst = ..numberAGet() / ..numberBGet()
q reulst
}
}
Class PHA.YX.Design.SimpleFactory.OperationIndex Extends PHA.YX.Design.SimpleFactory.Operation
{
Method GetResult()
{
s reulst = 0
s reulst = ..numberAGet() ** ..numberBGet()
q reulst
}
}
厂家可以生产各种品牌电脑,例如hp,lenovo,asus等品牌。简单工厂模式如何实现。感兴趣的同学实现后可以发我一起参考下。