1. Strategy Pattern

Strategy behavioral design pattern enables an algorithm's behavior to be selected at runtime.

It defines algorithms, encapsulates them, and uses them interchangeably.

1.1. Implementation

Implementation of an interchangeable operator object that operates on integers.

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)
}

1.2. Usage

1.2.1. Addition Operator

type Addition struct{}

func (Addition) Apply(lval, rval int) int {
    return lval + rval
}
add := Operation{Addition{}}
add.Operate(3, 5) // 8

1.2.2. Multiplication Operator

type Multiplication struct{}

func (Multiplication) Apply(lval, rval int) int {
    return lval * rval
}
mult := Operation{Multiplication{}}

mult.Operate(3, 5) // 15

1.3. Rules of Thumb

  • Strategy pattern is similar to Template pattern except in its granularity.
  • Strategy pattern lets you change the guts of an object. Decorator pattern lets you change the skin.
Copyright © studygolang.com 2013 all right reserved,powered by Gitbook该文件修订时间: 2017-07-21 20:12:31

results matching ""

    No results matching ""