This repository demonstrates the application of the Flyweight design pattern in Go. The project highlights how to minimize memory usage by sharing as much data as possible with other similar objects, showcasing flexibility and best practices in design patterns and unit testing.
The Flyweight pattern is used to minimize memory usage by sharing data among similar objects. This is achieved by storing common data externally and passing it to the objects that need it. In this project, we have implemented the Flyweight pattern using a FlyweightFactory
to manage and reuse flyweight objects.
- cmd/: Contains the application entry point (
main.go
), demonstrating the creation and usage of flyweights. - pkg/flyweight/: Houses the Flyweight implementation.
- flyweight.go: Defines the
Flyweight
interface. - concrete_flyweight.go: Implements the
ConcreteFlyweight
. - flyweight_factory.go: Implements the
FlyweightFactory
to create and manage flyweight objects. - concrete_flyweight_test.go: Unit tests for
ConcreteFlyweight
. - flyweight_factory_test.go: Unit tests for
FlyweightFactory
.
- flyweight.go: Defines the
Ensure you have Go installed on your system. You can download it from Go's official site.
Clone this repository to your local machine:
git clone https://github.com/arthurfp/Go_Flyweight_Pattern.git
cd Go_Flyweight_Pattern
To run the application, execute:
go run cmd/main.go
To execute the tests and verify the functionality:
go test ./...
Contributions are welcome! Please feel free to submit pull requests or open issues to discuss proposed changes or enhancements.
Arthur Ferreira - github.com/arthurfp