Skip to content

Instantly share code, notes, and snippets.

@arriqaaq
Created January 27, 2023 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arriqaaq/f3867b391ac657e4af7851f579a5f5a5 to your computer and use it in GitHub Desktop.
Save arriqaaq/f3867b391ac657e4af7851f579a5f5a5 to your computer and use it in GitHub Desktop.
// Font is the flyweight object
type Font struct {
fontType string
fontSize int
fontColor string
}
// FontFactory is the flyweight factory
type FontFactory struct {
fonts map[string]*Font
}
func (f *FontFactory) GetFont(fontType string, fontSize int, fontColor string) *Font {
key := fontType + "-" + strconv.Itoa(fontSize) + "-" + fontColor
if f.fonts == nil {
f.fonts = make(map[string]*Font)
}
if font, ok := f.fonts[key]; ok {
return font
}
font := &Font{fontType: fontType, fontSize: fontSize, fontColor: fontColor}
f.fonts[key] = font
return font
}
// Text is the client
type Text struct {
content []string
font *Font
fontType string
fontSize int
fontColor string
}
func (t *Text) AddWord(word string) {
t.content = append(t.content, word)
}
func (t *Text) SetFont(fontType string, fontSize int, fontColor string) {
t.fontType = fontType
t.fontSize = fontSize
t.fontColor = fontColor
t.font = fontFactory.GetFont(fontType, fontSize, fontColor)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment