{"id":21192229,"url":"https://github.com/bujosa/flyweight-design-pattern","last_synced_at":"2025-08-28T02:48:59.672Z","repository":{"id":220890079,"uuid":"752536481","full_name":"bujosa/flyweight-design-pattern","owner":"bujosa","description":"See example of flyweight design pattern in go","archived":false,"fork":false,"pushed_at":"2024-02-04T23:43:01.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-22T04:05:00.702Z","etag":null,"topics":["design-patterns","flyweight-pattern","golang","structural-patterns"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bujosa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-02-04T05:58:10.000Z","updated_at":"2024-02-04T23:56:37.000Z","dependencies_parsed_at":"2024-02-05T01:37:59.861Z","dependency_job_id":"61d92747-f126-47c8-98f4-69fb45f3db8b","html_url":"https://github.com/bujosa/flyweight-design-pattern","commit_stats":null,"previous_names":["bujosa/flyweight-design-pattern"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bujosa/flyweight-design-pattern","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bujosa%2Fflyweight-design-pattern","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bujosa%2Fflyweight-design-pattern/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bujosa%2Fflyweight-design-pattern/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bujosa%2Fflyweight-design-pattern/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bujosa","download_url":"https://codeload.github.com/bujosa/flyweight-design-pattern/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bujosa%2Fflyweight-design-pattern/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272424886,"owners_count":24932895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-28T02:00:10.768Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["design-patterns","flyweight-pattern","golang","structural-patterns"],"created_at":"2024-11-20T19:08:02.228Z","updated_at":"2025-08-28T02:48:59.547Z","avatar_url":"https://github.com/bujosa.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flyweight Design Pattern\n\nThe Flyweight design pattern is a structural design pattern that provides a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. It's particularly useful when you need to spawn a large number of similar objects.\n\nIn the provided Go code, the Flyweight pattern is used to manage `Team` objects.\n\n## Code Explanation\n### Constants\n\n```go\nconst (\n    TEAM_A = iota\n    TEAM_B\n)\n```\n\nThese constants represent the two types of teams that can be created.\n\n### Structs\n\nSeveral structs are defined: `Player`, `HistoricalData`, `Team`, and `Match`. These represent different entities in a football league.\n\n### Flyweight Factory\n\nThe `teamFlyweightFactory` struct is the Flyweight Factory. It maintains a map of already created teams.\n\n```go\ntype teamFlyweightFactory struct {\n    createdTeams map[int]*Team\n}\n```\n\n### Factory Methods\n\nThe `getTeamFactory` function is a simple factory method that creates a new `Team` based on the input.\n\n```go\nfunc getTeamFactory(team int) Team {\n    //...\n}\n```\n\nThe `NewTeamFactory` function creates a new instance of `teamFlyweightFactory`.\n\n```go\nfunc NewTeamFactory() teamFlyweightFactory {\n    //...\n}\n```\n\n### Flyweight Methods\n\nThe `GetTeam` method in `teamFlyweightFactory` checks if a team already exists in the `createdTeams` map. If it does, it returns the existing team. If not, it creates a new team, adds it to the map, and then returns it. This is where the Flyweight pattern is implemented.\n\n```go\nfunc (t *teamFlyweightFactory) GetTeam(teamName int) *Team {\n    //...\n}\n```\n\nThe `GetNumberOfObjects` method returns the number of teams created.\n\n```go\nfunc (t *teamFlyweightFactory) GetNumberOfObjects() int {\n    //...\n}\n```\n\n## Usage\n\nThe Flyweight pattern is used when you need to create a large number of similar objects. In this case, it's used to manage `Team` objects in a football league. By using the Flyweight pattern, you can reduce memory usage by sharing common parts of the state between multiple objects instead of keeping all of the data in each object.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbujosa%2Fflyweight-design-pattern","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbujosa%2Fflyweight-design-pattern","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbujosa%2Fflyweight-design-pattern/lists"}