flyweight.go

package flyweight

import (
	"time"
)

const (
	TEAM_A = "A"
	TEAB_B = "B"
)

type Team struct {
	ID             uint64
	Name           string
	Shield         []byte
	Players        []Player
	HistoricalData []HistoricalData
}

type Player struct {
	Name         string
	Surname      string
	PerviousTeam uint64
	Photo        []byte
}

type HistoricalData struct {
	Year          uint8
	LeagueResults []Match
}

type Match struct {
	Date          time.Time
	VisitorID     uint64
	LocalID       uint64
	LocalScore    byte
	VisitorScore  byte
	LocalShoots   uint16
	VisitorShoots uint16
}

type teamFlyweightFactory struct {
	createdTeams map[string]*Team
}

func (t *teamFlyweightFactory) GetTeam(teamID string) *Team {
	if t.createdTeams[teamID] != nil {
		return t.createdTeams[teamID]
	}

	team := getTeamFactory(teamID)
	t.createdTeams[teamID] = &team
	return t.createdTeams[teamID]
}

func getTeamFactory(team string) Team {
	switch team {
	case TEAB_B:
		return Team{
			ID:   2,
			Name: TEAB_B,
		}
	default:
		return Team{
			ID:   1,
			Name: TEAM_A,
		}
	}
}

func (t *teamFlyweightFactory) GetNumberOfObjects() int {
	return len(t.createdTeams)
}

func NewTeamFactory() teamFlyweightFactory {
	return teamFlyweightFactory{
		createdTeams: make(map[string]*Team),
	}
}

  

flyweight_test.go

package flyweight

import (
	"fmt"
	"testing"
)

func TestTeamFlyweightFactory_GetTeam(t *testing.T) {
	factory := NewTeamFactory()

	teamA1 := factory.GetTeam(TEAM_A)
	if teamA1 == nil {
		t.Error("The pointer to the TEAM_A was nil")
	}
	teamA2 := factory.GetTeam(TEAM_A)
	if teamA2 == nil {
		t.Error("The pointer to the TEAM_A was nil")
	}

	if teamA1 != teamA2 {
		t.Error("TEAM_A pointers weren't the same")
	}

	if factory.GetNumberOfObjects() != 1 {
		t.Errorf("The number of objects created was not 1: %d\n", factory.GetNumberOfObjects())
	}

}

func Test_HighVolume(t *testing.T) {
	factory := NewTeamFactory()
	teams := make([]*Team, 5000*2)
	for i := 0; i < 5000; i++ {
		teams[i] = factory.GetTeam(TEAM_A)
	}
	for i := 5000; i < 2*5000; i++ {
		teams[i] = factory.GetTeam(TEAB_B)
	}

	if factory.GetNumberOfObjects() != 2 {
		t.Errorf("The number of objects created was not 2: %d\n", factory.GetNumberOfObjects())
	}

	for i := 0; i < 3; i++ {
		fmt.Printf("Pointer %d points to %p and is located in %p\n", i, teams[i], &teams[i])
	}
}

  

最新文章

  1. CSS3-html,样式与样式表的创建,选择器
  2. hadoop错误之ClassNotFoundException
  3. uva 10617
  4. Javascript String类的属性及方法
  5. 案例:latch: cache buffers chains event tuning
  6. Unity Manual 用户手册
  7. QT5-控件-QSpinBox和QDoubleSpinBox(用于通过控件调整整数和小数)
  8. Go语言之异常处理
  9. haproxy 访问www.zjdev.com 自动跳转到appserver_8001 对应的nginx
  10. Java Swing TextArea 滚动条和获得焦点
  11. CSV文件格式分析器执行:从字符串Split至FSM
  12. EF OrderBy(string propertyname), OrderByDescending(string propertyname) 按属性排序,扩展方法
  13. Extjs6获取Grid里的数据(数据源)
  14. Pycharm启动后加载anaconda一直updating indices造成Pycharm闪退甚至电脑崩溃
  15. C# 动态调用WebService 2
  16. centos7编译安装zabbix(附带编译安装lnmp)
  17. poj2828 线段树单点更新
  18. Ajax 小实例
  19. python3之Django模型(一)
  20. [Ubuntu] 如何设置静态 IP 和 DNS

热门文章

  1. c++多个文件中共用一个全局变量 变量跨文件使用
  2. SpringCloud找不到@HystrixCommand标签
  3. 设计模式之单例模式(Java)
  4. 有抱负的 DevOps 和 SRE 工程师必读好书清单 | 文末有福利!
  5. Node接口实现HTTPS版的
  6. JavaScript-打印倒三角形和正三角形
  7. [C]表达式结合规律和运算符优先级
  8. SpringCloud学习笔记(十一、SpringCloud总结)
  9. ubuntu下面安装nodejs
  10. PHP捕获异常register_shutdown_function和error_get_last的使用