https://github.com/robert076/strategy-pattern
Trying to get used to the strategy design pattern, hence this practice repo. Code in Go.
https://github.com/robert076/strategy-pattern
design-patterns golang refactoring-guru strategy-design-pattern
Last synced: 3 months ago
JSON representation
Trying to get used to the strategy design pattern, hence this practice repo. Code in Go.
- Host: GitHub
- URL: https://github.com/robert076/strategy-pattern
- Owner: Robert076
- License: apache-2.0
- Created: 2025-06-14T13:14:27.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-06-14T13:45:32.000Z (7 months ago)
- Last Synced: 2025-06-14T14:35:07.082Z (7 months ago)
- Topics: design-patterns, golang, refactoring-guru, strategy-design-pattern
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🧩 strategy-pattern
Trying to get used to the strategy design pattern, hence this practice repo. Followed tutorial on refactoring.guru.
This design pattern is recommended to be used in the case of long if statements, where there are ifs after ifs. For example in my message code if I had the Message struct as:
```
type Message struct {
text string
sendstrategy string
}
```
instead of
```
type Message struct {
text string
sendstrategy sendstrategy
}
```
Then every time I sent a message I had to do:
```
*pseudocode*
if message.sendstrategy == "sms"
smsservice.send(message)
else if message.sendstrategy == "carrierpigeon"
carrierpigeonservice.send(message)
etc.
```
You notice how scaling the program gets increasingly harder as each new method adds another if statement.
But if instead we use a strategy no new code is needed except for the implementation of the new strategy, and it can be then passed down if it implements the strategy interface.
Making the final code just:
```
Message.send()
```
That's it!
Very useful pattern.