-
Notifications
You must be signed in to change notification settings - Fork 74
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
160eac3
commit 9992c58
Showing
48 changed files
with
1,488 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,97 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
// BookType represents the type of book | ||
type BookType int | ||
|
||
// Predefined Book types | ||
const ( | ||
HardCover BookType = iota | ||
SoftCover | ||
PaperBack | ||
EBook | ||
) | ||
|
||
// Book represents data about a book | ||
type Book struct { | ||
Name string | ||
Author string | ||
PageCount int | ||
Type BookType | ||
} | ||
|
||
// Library holds the collection of books | ||
type Library struct { | ||
Collection []Book | ||
} | ||
|
||
// IterateBooks calls the given callback function | ||
// for each book in the collection | ||
func (l *Library) IterateBooks(f func(Book) error) { | ||
var err error | ||
for _, b := range l.Collection { | ||
err = f(b) | ||
if err != nil { | ||
fmt.Println("Error encountered:", err) | ||
break | ||
} | ||
} | ||
} | ||
|
||
// createIterator returns a BookIterator that can access the book | ||
// collection on demand | ||
func (l *Library) createIterator() iterator { | ||
return &BookIterator{ | ||
books: l.Collection, | ||
} | ||
} | ||
|
||
// ------------------- | ||
// Create a Library structure to hold a set of Books | ||
var lib *Library = &Library{ | ||
Collection: []Book{ | ||
{ | ||
Name: "War and Peace", | ||
Author: "Leo Tolstoy", | ||
PageCount: 864, | ||
Type: HardCover, | ||
}, | ||
{ | ||
Name: "Crime and Punishment", | ||
Author: "Leo Tolstoy", | ||
PageCount: 1225, | ||
Type: SoftCover, | ||
}, | ||
{ | ||
Name: "Brave New World", | ||
Author: "Aldous Huxley", | ||
PageCount: 325, | ||
Type: PaperBack, | ||
}, | ||
{ | ||
Name: "Catcher in the Rye", | ||
Author: "J.D. Salinger", | ||
PageCount: 206, | ||
Type: HardCover, | ||
}, | ||
{ | ||
Name: "To Kill a Mockingbird", | ||
Author: "Harper Lee", | ||
PageCount: 399, | ||
Type: PaperBack, | ||
}, | ||
{ | ||
Name: "The Grapes of Wrath", | ||
Author: "John Steinbeck", | ||
PageCount: 464, | ||
Type: HardCover, | ||
}, | ||
{ | ||
Name: "Wuthering Heights", | ||
Author: "Emily Bronte", | ||
PageCount: 288, | ||
Type: EBook, | ||
}, | ||
}, | ||
} |
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,28 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
// Iterate using a callback function | ||
func main() { | ||
// use IterateBooks to iterate via a callback function | ||
lib.IterateBooks(myBookCallback) | ||
|
||
// Use IterateBooks to iterate via anonymous function | ||
lib.IterateBooks(func(b Book) error { | ||
fmt.Println("Book author:", b.Author) | ||
return nil | ||
}) | ||
|
||
// create a BookIterator | ||
iter := lib.createIterator() | ||
for iter.hasNext() { | ||
book := iter.next() | ||
fmt.Printf("Book %+v\n", book) | ||
} | ||
} | ||
|
||
// This callback function processes an individual Book object | ||
func myBookCallback(b Book) error { | ||
fmt.Println("Book title:", b.Name) | ||
return nil | ||
} |
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,36 @@ | ||
package main | ||
|
||
// The IterableCollection interface defines the createIterator | ||
// function, which returns an iterator object | ||
type IterableCollection interface { | ||
createIterator() iterator | ||
} | ||
|
||
// The iterator interface contains the hasNext and next functions | ||
// which allow the collection to return items as needed | ||
type iterator interface { | ||
hasNext() bool | ||
next() *Book | ||
} | ||
|
||
// BookIterator is a concrete iterator for a Book collection | ||
type BookIterator struct { | ||
current int | ||
books []Book | ||
} | ||
|
||
func (b *BookIterator) hasNext() bool { | ||
if b.current < len(b.books) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (b *BookIterator) next() *Book { | ||
if b.hasNext() { | ||
bk := b.books[b.current] | ||
b.current++ | ||
return &bk | ||
} | ||
return nil | ||
} |
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,28 @@ | ||
package main | ||
|
||
func main() { | ||
// Construct two DataListener observers and | ||
// give each one a name | ||
listener1 := DataListener{ | ||
Name: "Listener 1", | ||
} | ||
listener2 := DataListener{ | ||
Name: "Listener 2", | ||
} | ||
|
||
// Create the DataSubject that the listeners will observe | ||
subj := &DataSubject{} | ||
// Register each listener with the DataSubject | ||
subj.registerObserver(listener1) | ||
subj.registerObserver(listener2) | ||
|
||
// Change the data in the DataSubject - this will cause the | ||
// onUpdate method of each listener to be called | ||
subj.ChangeItem("Monday!") | ||
subj.ChangeItem("Wednesday!") | ||
|
||
// Try to unregister one of the observers | ||
subj.unregisterObserver(listener2) | ||
// Change the data again, now only the first listener is called | ||
subj.ChangeItem("Friday!") | ||
} |
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,18 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
// Define the interface for an observer type | ||
type observer interface { | ||
onUpdate(data string) | ||
} | ||
|
||
// Our DataListener observer will have a name | ||
type DataListener struct { | ||
Name string | ||
} | ||
|
||
// To conform to the interface, it must have an onUpdate function | ||
func (dl *DataListener) onUpdate(data string) { | ||
fmt.Println("Listener:", dl.Name, "got data change:", data) | ||
} |
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,45 @@ | ||
package main | ||
|
||
// Define the interface for the observable type | ||
type observable interface { | ||
register(obs observer) | ||
unregister(obs observer) | ||
notifyAll() | ||
} | ||
|
||
// The DataSubject will have a list of listeners | ||
// and a field that gets changed, triggering them | ||
type DataSubject struct { | ||
observers []DataListener | ||
field string | ||
} | ||
|
||
// The ChangeItem function will cause the Listeners to be called | ||
func (ds *DataSubject) ChangeItem(data string) { | ||
ds.field = data | ||
|
||
ds.notifyAll() | ||
} | ||
|
||
// This function adds an observer to the list | ||
func (ds *DataSubject) registerObserver(o DataListener) { | ||
ds.observers = append(ds.observers, o) | ||
} | ||
|
||
// This function removes an observer from the list | ||
func (ds *DataSubject) unregisterObserver(o DataListener) { | ||
var newobs []DataListener | ||
for _, obs := range ds.observers { | ||
if o.Name != obs.Name { | ||
newobs = append(newobs, obs) | ||
} | ||
} | ||
ds.observers = newobs | ||
} | ||
|
||
// The notifyAll function calls all the listeners with the changed data | ||
func (ds *DataSubject) notifyAll() { | ||
for _, obs := range ds.observers { | ||
obs.onUpdate(ds.field) | ||
} | ||
} |
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,51 @@ | ||
package main | ||
|
||
// The NotificationBuilder has fields exported as well as a few methods | ||
// to demonstrate | ||
type NotificationBuilder struct { | ||
Title string | ||
Message string | ||
Image string | ||
Icon string | ||
Priority int | ||
NotType string | ||
} | ||
|
||
func newNotificationBuilder() *NotificationBuilder { | ||
return &NotificationBuilder{} | ||
} | ||
|
||
func (nb *NotificationBuilder) SetTitle(title string) { | ||
nb.Title = title | ||
} | ||
|
||
func (nb *NotificationBuilder) SetMessage(message string) { | ||
nb.Message = message | ||
} | ||
|
||
func (nb *NotificationBuilder) SetImage(image string) { | ||
nb.Image = image | ||
} | ||
|
||
func (nb *NotificationBuilder) SetIcon(icon string) { | ||
nb.Icon = icon | ||
} | ||
|
||
func (nb *NotificationBuilder) SetPriority(pri int) { | ||
nb.Priority = pri | ||
} | ||
|
||
func (nb *NotificationBuilder) SetType(notType string) { | ||
nb.NotType = notType | ||
} | ||
|
||
func (nb *NotificationBuilder) Build() (Notification, error) { | ||
return Notification{ | ||
title: nb.Title, | ||
message: nb.Message, | ||
image: nb.Image, | ||
icon: nb.Icon, | ||
priority: nb.Priority, | ||
notType: nb.NotType, | ||
}, nil | ||
} |
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 main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
var bldr = newNotificationBuilder() | ||
bldr.SetTitle("New Notification") | ||
bldr.SetIcon("icon 1") | ||
bldr.SetMessage("This is a basic notification") | ||
|
||
notif, _ := bldr.Build() | ||
fmt.Printf("Notification: %+v\n", notif) | ||
} |
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,11 @@ | ||
package main | ||
|
||
// This is the finished product that is created by the builder | ||
type Notification struct { | ||
title string | ||
message string | ||
image string | ||
icon string | ||
priority int | ||
notType string | ||
} |
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,25 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
mag1, _ := newPublication("magazine", "Tyme", 50, "The Tymes") | ||
mag2, _ := newPublication("magazine", "Lyfe", 40, "Lyfe Inc") | ||
news1, _ := newPublication("newspaper", "The Herald", 60, "Heralders") | ||
news2, _ := newPublication("newspaper", "The Standard", 30, "Standarders") | ||
|
||
pubDetails(mag1) | ||
pubDetails(mag2) | ||
pubDetails(news1) | ||
pubDetails(news2) | ||
} | ||
|
||
func pubDetails(pub iPublication) { | ||
fmt.Printf("--------------------\n") | ||
fmt.Printf("%s\n", pub) | ||
fmt.Printf("Type: %T\n", pub) | ||
fmt.Printf("Name: %s\n", pub.getName()) | ||
fmt.Printf("Pages: %d\n", pub.getPages()) | ||
fmt.Printf("Publisher: %s\n", pub.getPublisher()) | ||
fmt.Printf("--------------------\n") | ||
} |
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,14 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func newPublication(pubType string, name string, pg int, pub string) (iPublication, error) { | ||
// Create the right kind of publication based on the given type | ||
if pubType == "newspaper" { | ||
return createNewspaper(name, pg, pub), nil | ||
} | ||
if pubType == "magazine" { | ||
return createMagazine(name, pg, pub), nil | ||
} | ||
return nil, fmt.Errorf("No such publication type") | ||
} |
Oops, something went wrong.