Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hendarsu/go-factory-method
https://github.com/hendarsu/go-factory-method
Last synced: 22 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/hendarsu/go-factory-method
- Owner: hendarSu
- Created: 2022-02-15T16:32:46.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-02-15T17:59:31.000Z (almost 3 years ago)
- Last Synced: 2024-11-01T23:24:58.702Z (2 months ago)
- Language: Go
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Conceptual Example
#### Reference : https://refactoring.guru/design-patterns/factory-method/go/example
It’s impossible to implement the classic Factory Method pattern in Go due to lack of OOP features such as classes and inheritance. However, we can still implement the basic version of the pattern, the Simple Factory.
In this example, we’re going to build various types of weapons using a factory struct.
First, we create the iGun interface, which defines all methods a gun should have. There is a gun struct type that implements the iGun interface. Two concrete guns—ak47 and musket—both embed gun struct and indirectly implement all iGun methods.
The gunFactory struct serves as a factory, which creates guns of the desired type based on an incoming argument. The main.go acts as a client. Instead of directly interacting with ak47 or musket, it relies on gunFactory to create instances of various guns, only using string parameters to control the production.