1
0
mirror of https://github.com/tmrts/go-patterns.git synced 2024-11-28 16:06:07 +03:00

added generic request arguements for process method.

This commit is contained in:
akshay.nambiar 2019-03-14 12:10:40 +07:00
parent 33cddd7340
commit 69c323be3c

View File

@ -5,9 +5,13 @@ import (
"fmt" "fmt"
) )
type request struct {
value float64
}
//NumberProcessor has the processor method which will tell if the value is negative, positive or zero //NumberProcessor has the processor method which will tell if the value is negative, positive or zero
type NumberProcessor interface { type NumberProcessor interface {
process(int) process(request)
} }
//ZeroHandler handles the value which is zero //ZeroHandler handles the value which is zero
@ -26,26 +30,27 @@ type NegativeHandler struct {
} }
//For returning zero if the value is zero. //For returning zero if the value is zero.
func (zero ZeroHandler) process(value int) { func (zero ZeroHandler) process(req request) {
if value == 0 {
if req.value == 0.0 {
fmt.Print("its zero") fmt.Print("its zero")
} else { } else {
zero.numberProcessor.process(value) zero.numberProcessor.process(req)
} }
} }
//For returning its negative if the value is negative. //For returning its negative if the value is negative.
func (negative NegativeHandler) process(value int) { func (negative NegativeHandler) process(req request) {
if value < 0 { if req.value < 0 {
fmt.Print("its a negative number") fmt.Print("its a negative number")
} else { } else {
negative.numberProcessor.process(value) negative.numberProcessor.process(req)
} }
} }
//For returning its positive if the value is positive. //For returning its positive if the value is positive.
func (positve PositiveHandler) process(value int) { func (positve PositiveHandler) process(req request) {
if value > 0 { if req.value > 0 {
fmt.Print("its a postitive number") fmt.Print("its a postitive number")
} }
} }
@ -53,7 +58,8 @@ func (positve PositiveHandler) process(value int) {
func main() { func main() {
//initialising the chain of actions. //initialising the chain of actions.
zeroHandler := ZeroHandler{NegativeHandler{PositiveHandler{}}} zeroHandler := ZeroHandler{NegativeHandler{PositiveHandler{}}}
zeroHandler.process(10)
zeroHandler.process(-19) zeroHandler.process(request{10})
zeroHandler.process(0) zeroHandler.process(request{-19.9})
zeroHandler.process(request{0})
} }