享元模式
享元模式的意图是复用对象,节省内存,前提是享元对象是不可变对象。
享元:对象级别的复用;也可以是对象的字段进行复用
实现方式:主要通过工厂模式,在工厂类中,通过一个Map来缓存已经创建过的享元对象,来达到复用的目的
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package design
type ChessPieceUnit struct { ID uint Name string Color string }
var units = map[int]*ChessPieceUnit{ 1: { ID: 1, Name: "車", Color: "red", }, 2: { ID: 2, Name: "炮", Color: "red", }, }
func NewChessPieceUnit(id int) *ChessPieceUnit { return units[id] }
type ChessPiece struct { Unit *ChessPieceUnit X int Y int }
type ChessBoard struct { chessPieces map[int]*ChessPiece }
func NewChessBoard() *ChessBoard { board := &ChessBoard{chessPieces: map[int]*ChessPiece{}} for id := range units { board.chessPieces[id] = &ChessPiece{ Unit: NewChessPieceUnit(id), X: 0, Y: 0, } } return board }
func (c *ChessBoard) Move(id, x, y int) { c.chessPieces[id].X = x c.chessPieces[id].Y = y }
|
单元测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package design
import ( "testing" "github.com/stretchr/testify/assert" )
func TestNewChessBoard(t *testing.T) { board1 := NewChessBoard() board1.Move(1, 1, 2) board2 := NewChessBoard() board2.Move(2, 2, 3)
assert.Equal(t, board1.chessPieces[1].Unit, board2.chessPieces[1].Unit) assert.Equal(t, board1.chessPieces[2].Unit, board2.chessPieces[2].Unit) }
|