Skip to content

Commit 17aee29

Browse files
Added factor method to create new matchers
1 parent 7ca8d9d commit 17aee29

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

‎src/chapter2/search/main.go‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ import (
99
"github.com/goinaction/code/src/chapter2/search/rss"
1010
)
1111

12+
// NewMatcher is a factory that creates matcher values based
13+
// on the type of feed specified.
14+
func NewMatcher(feed data.Feed) search.Matcher {
15+
// Create the right type of matcher for this search.
16+
switch feed.Type {
17+
case "rss":
18+
return rss.NewMatcher(&feed)
19+
20+
// TODO: Add new Matchers here
21+
}
22+
23+
log.Fatalln("Invalid Feed Type")
24+
return nil
25+
}
26+
1227
// main is the entry point for the program.
1328
func main() {
1429
// Search term we are looking for.
@@ -29,16 +44,8 @@ func main() {
2944

3045
// Launch a goroutine for each feed to find the results.
3146
for _, feed := range feeds {
32-
var matcher search.Matcher
33-
34-
// Create the right type of matcher for this search.
35-
switch feed.Type {
36-
case "rss":
37-
matcher = rss.NewMatcher(feed)
38-
39-
default:
40-
log.Fatalln("Invalid Feed Type")
41-
}
47+
// Create a matcher for the search.
48+
matcher := NewMatcher(feed)
4249

4350
// Launch the goroutine to perform the search.
4451
go func() {

‎src/chapter2/search/rss/rss.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ type (
6262
)
6363

6464
// NewMatcher creates a value of matcher for use.
65-
func NewMatcher(feed data.Feed) feed.Matcher {
65+
func NewMatcher(feed *data.Feed) feed.Matcher {
6666
return &matcher{
67-
Feed: &feed,
67+
Feed: feed,
6868
}
6969
}
7070

0 commit comments

Comments
 (0)