Golang设计模式之备忘录模式

Jackey Golang 2,333 次浏览 , , 没有评论

创建文件MMFeel.go

package memento

import "fmt"

type MMFeel struct {
    Tall     int
    Rich     int
    Handsome int
}

var States []MMFeel = make([]MMFeel, 0)
var StatesLength int = 0 // 记录轨迹

func (mmf *MMFeel) FirstMeet(tall, rich, handsome int) {
    mmf.Tall = tall
    mmf.Rich = rich
    mmf.Handsome = handsome
    States = append(States, *mmf)
}

func (mmf *MMFeel) GoKorea() {
    mmf.Handsome += 30
    States = append(States, *mmf)
}

func (mmf *MMFeel) WinLottery() {
    mmf.Rich += 5000000
    States = append(States, *mmf)
}

func (mmf *MMFeel) Short() {
    mmf.Tall += 10
    States = append(States, *mmf)
}

func (mmf *MMFeel) Score() {
    fmt.Println("当前状态", mmf.Tall, mmf.Rich, mmf.Handsome)
}

main.go

package main

import (
    "fmt"
    "ssp_api_go/test/design/memento"
)

func main() {
    var xxl *memento.MMFeel = &memento.MMFeel{0, 0, 0}
    xxl.FirstMeet(170, 500000, 70)
    xxl.WinLottery()
    xxl.GoKorea()
    xxl.Short()
    xxl.Score()
    fmt.Println(memento.States)
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Go