- vừa được xem lúc

[ Design Patterns ] - Observer pattern with Golang

0 0 16

Người đăng: Nguyen Van Tuan

Theo Viblo Asia

Ha Noi, Wed 10/05/2023

At the moment, a little speech to give to the present...

Cơn mưa ngang qua mang em đi xa

Cơn mưa ngang qua khiến em nhạt nhòa...

Today, I listened to Son Tung's old song. I missed something in the past. At the same time as this song, I read a story on Voz that's name is "[Kể lại 1 câu chuyện] Đặt tên là "CƠN MƯA NGANG QUA". This story is very perfect, let's read it! 😊😊😊

I. Observer Pattern

Observer is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.

image.png

Reference: Observer Pattern

II. Implement

1. The problem

image.png

Imagine that we have two objects: customers and the Apple Store.

The customer is a big fan of Apple, and he always wants to have the newest brand model whenever Apple introduces a new brand model. So, he must go to the Apple Store every day and ask the Apple Store's employees about the new brand model of Apple. Maybe it is still not available in the Apple Store, so he must spend lots of days going to the store.

Hmm, as in the image above, to resolve his problem, the store will notify and email all customers whenever the new brand model is available in the store. Nicely, the solution resolved his problem about spending time going to the store. But some customers like Android phones, so whenever they receive a notification about the new brand model from Apple, they are very angry and not happy.

2. The solution

image.png

So, to resolve this problem for him and some other customers who like Android phones, the store created a registration form to buy the new brand model. Whenever the product is available, the store will send a notification to all customers registered to buy iPhones.

So, to resolve the problem, we will use the Observer Pattern.

3. Implement Observer Pattern

3.1. Interface Publisher

ipublisher.go: This is the action interface of the publisher.

package publisher import "design-pattern-golang-example/observer_pattern/subscriber" type IPublisher interface { Subscribe(s subscriber.ISubscriber) UnSubscribe(s subscriber.ISubscriber) NotifySubscribes()
}

3.2. Concrete Publisher

apple_store.go: The publisher will handle and implement the logic for all actions that define by the interface publisher. Here, I define a publisher as Apple Store.

package concrete_publisher import ( "fmt" "time" "design-pattern-golang-example/observer_pattern/publisher" "design-pattern-golang-example/observer_pattern/subscriber"
) type AppleStore struct { Subscribers []subscriber.ISubscriber Name string
} func NewAppleStore(subscribers []subscriber.ISubscriber, name string) publisher.IPublisher { return &AppleStore{ Subscribers: subscribers, Name: name, }
} func (as *AppleStore) Subscribe(s subscriber.ISubscriber) { as.Subscribers = append(as.Subscribers, s) time.Sleep(1 * time.Second) fmt.Println(s.GetEmail() + " was subscribed...")
} func (as *AppleStore) UnSubscribe(s subscriber.ISubscriber) { subscribersUpdate := make([]subscriber.ISubscriber, 0) for _, sub := range as.Subscribers { if sub.GetEmail() != s.GetEmail() { subscribersUpdate = append(subscribersUpdate, sub) } } time.Sleep(1 * time.Second) fmt.Println(s.GetEmail() + " was unsubscribed...") as.Subscribers = subscribersUpdate
} func (as *AppleStore) NotifySubscribes() { for _, sub := range as.Subscribers { sub.SendEmail("We have had a new model of the iPhone") time.Sleep(1 * time.Second) }
}

3.3. Interface Subscriber

isubscriber.go: This is the action interface of the subscribers.

package subscriber // ISubscriber this is the observer
type ISubscriber interface { SendEmail(notification string) GetEmail() string
}

3.4. Concrete Subscriber

customer_subscriber.go: The subscriber will handle and implement the logic for all actions that define by the interface subscriber. Here, I define a subscriber as a customer.

package concrete_subscriber import ( "fmt" "design-pattern-golang-example/observer_pattern/subscriber"
) type Customer struct { Email string
} func NewCustomer(email string) subscriber.ISubscriber { return &Customer{Email: email}
} func (c *Customer) SendEmail(notification string) { content := fmt.Sprintf(`Sending the notification for %s with content "%s"`, c.GetEmail(), notification) fmt.Println(content)
} func (c *Customer) GetEmail() string { return c.Email
}

3.5. Main Function

main.go: Hàm chạy chương trình

package main import ( "time" "design-pattern-golang-example/observer_pattern/publisher/concrete_publisher" "design-pattern-golang-example/observer_pattern/subscriber" "design-pattern-golang-example/observer_pattern/subscriber/concrete_subscriber"
) func main() { // init publisher appleStore := concrete_publisher.NewAppleStore([]subscriber.ISubscriber{}, "apple store") // init observer subscriberOne := concrete_subscriber.NewCustomer("subscribe-one@gmail.com") subscriberTwo := concrete_subscriber.NewCustomer("subscribe-two@gmail.com") // the observer subscribes publisher appleStore.Subscribe(subscriberOne) appleStore.Subscribe(subscriberTwo) // send notification time.Sleep(3 * time.Second) appleStore.NotifySubscribes() // the observer unsubscribes publisher appleStore.UnSubscribe(subscriberTwo) // send notification time.Sleep(3 * time.Second) appleStore.NotifySubscribes()
}

Result

subscribe-one@gmail.com was subscribed...
subscribe-two@gmail.com was subscribed...
Sending the notification for subscribe-one@gmail.com with content "We have had a new model of the iPhone"
Sending the notification for subscribe-two@gmail.com with content "We have had a new model of the iPhone"
subscribe-two@gmail.com was unsubscribed...
Sending the notification for subscribe-one@gmail.com with content "We have had a new model of the iPhone" Process finished with the exit code 0

III. Source code

Bình luận

Bài viết tương tự

- vừa được xem lúc

gRPC - Nó là gì và có nên sử dụng hay không?

Nhân một ngày rảnh rỗi, mình ngồi đọc lại RPC cũng như gRPC viết lại để nhớ lâu hơn. Vấn đề là gì và tại sao cần nó .

0 0 112

- vừa được xem lúc

Embedded Template in Go

Getting Start. Part of developing a web application usually revolves around working with HTML as user interface.

0 0 40

- vừa được xem lúc

Tạo Resful API đơn giản với Echo framework và MySQL

1. Giới thiệu.

0 0 44

- vừa được xem lúc

Sử dụng goquery trong golang để crawler thông tin các website Việt Nam bị deface trên mirror-h.org

. Trong bài viết này, mình sẽ cùng mọi người khám phá một package thu thập dữ liệu có tên là goquery của golang. Mục tiêu chính của chương trình crawler này sẽ là lấy thông tin các website Việt Nam bị deface (là tấn công, phá hoại website, làm thay đổi giao diện hiển thị của một trang web, khi người

0 0 218

- vừa được xem lúc

Tạo ứng dụng craw dữ liệu bing với Golang, Mysql driver

Chào mọi người . Lâu lâu ta lại gặp nhau 1 lần, để tiếp tục series chia sẻ kiến thức về tech, hôm nay mình sẽ tìm hiểu và chia sẻ về 1 ngôn ngữ đang khá hot trong cộng đồng IT đó là Golang.

0 0 59

- vừa được xem lúc

Golang: Rest api and routing using MUX

Routing with MUX. Let's create a simple CRUD api for a blog site. # All . GET articles/ .

0 0 40