Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ypankaj007/design-patterns-in-go
Implementation of design patterns in Golang
https://github.com/ypankaj007/design-patterns-in-go
design-pattern go
Last synced: 25 days ago
JSON representation
Implementation of design patterns in Golang
- Host: GitHub
- URL: https://github.com/ypankaj007/design-patterns-in-go
- Owner: ypankaj007
- Created: 2019-06-18T11:17:48.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-05T10:06:10.000Z (almost 5 years ago)
- Last Synced: 2024-06-21T04:58:39.525Z (7 months ago)
- Topics: design-pattern, go
- Language: Go
- Homepage:
- Size: 77.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Observer Design Pattern in GO
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
# Example
- In the code, I took a simple message posting example over social media (Linkedin, Facebook and Twitter).
- User will input the message by command line and it will observer in all social media portal.
# Implementation- import observer package in our application
- Create Event and Register Observer
- Resiter observer
```sh
privatePortal.Attach(observer) // observer defines Observer object```
- Remove observer
```sh
privatePortal.Detach(observer) // observer defines Observer object```
- Notify to observer
```sh
privatePortal.Notify(e) // e defines Event object```
```sh
package mainimport (
"bufio"
ob "design-patterns-in-go/behavioral/observer"
"fmt"
"os"
)func main() {
fmt.Println("Observer application .......")
// Initialize notifier
privatePortal := ob.Initialize()
fmt.Println("Registering linkedin observer")
privatePortal.Attach(&ob.Linkedin{})fmt.Println("Registering facebook observer")
privatePortal.Attach(&ob.Facebook{})fmt.Println("Registering twitter observer")
privatePortal.Attach(&ob.Twitter{})
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
e := ob.Event{Message: scanner.Text()}
privatePortal.Notify(e)
}
fmt.Println(scanner.Err())
}```