mirror of
https://github.com/tmrts/go-patterns.git
synced 2024-11-22 04:56:09 +03:00
Add strategy pattern
This commit is contained in:
parent
cfd83425b6
commit
aeca31fe3d
65
functional_options.go
Normal file
65
functional_options.go
Normal file
@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import "os"
|
||||
|
||||
type arguments struct {
|
||||
UID int
|
||||
GID int
|
||||
Flags int
|
||||
Contents string
|
||||
Permissions os.FileMode
|
||||
}
|
||||
|
||||
type argument func(*arguments)
|
||||
|
||||
func UID(userID int) argument {
|
||||
return func(args *arguments) {
|
||||
args.UID = userID
|
||||
}
|
||||
}
|
||||
|
||||
func GID(groupID int) argument {
|
||||
return func(args *arguments) {
|
||||
args.GID = groupID
|
||||
}
|
||||
}
|
||||
|
||||
func Contents(c string) argument {
|
||||
return func(args *arguments) {
|
||||
args.Contents = c
|
||||
}
|
||||
}
|
||||
|
||||
func Permissions(perms os.FileMode) argument {
|
||||
return func(args *arguments) {
|
||||
args.Permissions = perms
|
||||
}
|
||||
}
|
||||
|
||||
func New(filepath string, setters ...argument) error {
|
||||
// Default arguments
|
||||
args := &arguments{
|
||||
UID: os.Getuid(),
|
||||
GID: os.Getgid(),
|
||||
Contents: "",
|
||||
Permissions: 0666,
|
||||
Flags: os.O_CREATE | os.O_EXCL | os.O_WRONLY,
|
||||
}
|
||||
|
||||
for _, setter := range setters {
|
||||
setter(args)
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(filepath, args.Flags, args.Permissions)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
defer f.Close()
|
||||
}
|
||||
|
||||
if _, err := f.WriteString(args.Contents); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return f.Chown(args.UID, args.GID)
|
||||
}
|
39
strategy.go
Normal file
39
strategy.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Operator interface {
|
||||
Apply(int, int) int
|
||||
}
|
||||
|
||||
type Operation struct {
|
||||
Operator Operator
|
||||
}
|
||||
|
||||
func (o *Operation) Operate(leftValue, rightValue int) int {
|
||||
return o.Operator.Apply(leftValue, rightValue)
|
||||
}
|
||||
|
||||
type Multiplication struct{}
|
||||
|
||||
func (Multiplication) Apply(lval, rval int) int {
|
||||
return lval * rval
|
||||
}
|
||||
|
||||
type Addition struct{}
|
||||
|
||||
func (Addition) Apply(lval, rval int) int {
|
||||
return lval + rval
|
||||
}
|
||||
|
||||
func main() {
|
||||
mult := Operation{Multiplication{}}
|
||||
|
||||
// Outputs 15
|
||||
fmt.Println(mult.Operate(3, 5))
|
||||
|
||||
pow := Operation{Addition{}}
|
||||
|
||||
// Outputs 8
|
||||
fmt.Println(pow.Operate(3, 5))
|
||||
}
|
48
template.go
48
template.go
@ -1,48 +0,0 @@
|
||||
// In Template pattern, an abstract struct exposes defined way(s)/template(s)
|
||||
// to execute its methods. This pattern comes under behavior pattern category.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Operator interface {
|
||||
Apply(int, int) int
|
||||
}
|
||||
|
||||
type Operation struct {
|
||||
Operator Operator
|
||||
}
|
||||
|
||||
func (o *Operation) Operate(leftValue, rightValue int) int {
|
||||
return o.Operator.Apply(leftValue, rightValue)
|
||||
}
|
||||
|
||||
type Multiplication struct{}
|
||||
|
||||
func (_ Multiplication) Apply(lval, rval int) int {
|
||||
return lval * rval
|
||||
}
|
||||
|
||||
type Pow struct{}
|
||||
|
||||
func (_ Pow) Apply(lval, rval int) int {
|
||||
return int(math.Pow(float64(lval), float64(rval)))
|
||||
}
|
||||
|
||||
func main() {
|
||||
mult := Operation{
|
||||
Operator: Multiplication{},
|
||||
}
|
||||
|
||||
// Outputs 15
|
||||
fmt.Println(mult.Operate(3, 5))
|
||||
|
||||
pow := Operation{
|
||||
Operator: Pow{},
|
||||
}
|
||||
|
||||
// Outputs 243
|
||||
fmt.Println(pow.Operate(3, 5))
|
||||
}
|
Loading…
Reference in New Issue
Block a user