Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c6bcc5
commit 688e38d
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package patterns | ||
|
||
import "fmt" | ||
|
||
// Subject interface | ||
type Subject interface { | ||
Register(Subscriber) | ||
Deregister(Subscriber) | ||
NotifyAll() | ||
} | ||
|
||
// Product struct | ||
type Product struct { | ||
SubscriberList []Subscriber | ||
name string | ||
inStock bool | ||
} | ||
|
||
// NewProduct function create a brand-new Product | ||
func NewProduct(name string) *Product { | ||
return &Product{ | ||
name: name, | ||
} | ||
} | ||
|
||
// UpdateAvailability method on Product struct | ||
func (i *Product) UpdateAvailability() { | ||
i.inStock = true | ||
i.NotifyAll() | ||
} | ||
|
||
// Register method on Product struct | ||
func (i *Product) Register(o Subscriber) { | ||
i.SubscriberList = append(i.SubscriberList, o) | ||
} | ||
|
||
// Deregister method on Product struct | ||
func (i *Product) Deregister(o Subscriber) { | ||
i.SubscriberList = removeFromslice(i.SubscriberList, o) | ||
} | ||
|
||
// NotifyAll method on Product struct | ||
func (i *Product) NotifyAll() { | ||
for _, Subscriber := range i.SubscriberList { | ||
Subscriber.Update(i.name) | ||
} | ||
} | ||
|
||
func removeFromslice(SubscriberList []Subscriber, observerToRemove Subscriber) []Subscriber { | ||
observerListLength := len(SubscriberList) | ||
for i, Subscriber := range SubscriberList { | ||
if observerToRemove.GetID() == Subscriber.GetID() { | ||
SubscriberList[observerListLength-1], SubscriberList[i] = SubscriberList[i], SubscriberList[observerListLength-1] | ||
return SubscriberList[:observerListLength-1] | ||
} | ||
} | ||
return SubscriberList | ||
} | ||
|
||
// Subscriber interface | ||
type Subscriber interface { | ||
Update(string) | ||
GetID() string | ||
} | ||
|
||
// Customer struct | ||
type Customer struct { | ||
id string | ||
} | ||
|
||
// Update method on Custome struct | ||
func (c *Customer) Update(itemName string) { | ||
fmt.Printf("Tell customer %s for new product %s\n", c.id, itemName) | ||
} | ||
|
||
// GetID method on Customer struct | ||
func (c *Customer) GetID() string { | ||
return c.id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package patterns | ||
|
||
func ExampleSubscriber() { | ||
shirtItem := NewProduct("Go React") | ||
firstCustomer := &Customer{id: "vue@yahoo.com"} | ||
secondCustomer := &Customer{id: "angular@yahoo.com"} | ||
shirtItem.Register(firstCustomer) | ||
shirtItem.Register(secondCustomer) | ||
shirtItem.UpdateAvailability() | ||
// Output: | ||
// Tell customer vue@yahoo.com for new product Go React | ||
// Tell customer angular@yahoo.com for new product Go React | ||
} |