Created
January 27, 2023 10:09
-
-
Save arriqaaq/f3867b391ac657e4af7851f579a5f5a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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