-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 786a0de
Showing
3 changed files
with
94 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,67 @@ | ||
package factory | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type Content interface { | ||
Play() | ||
} | ||
|
||
type CloudContent struct{} | ||
|
||
func (c *CloudContent) Play() { | ||
fmt.Println("this is cloud content") | ||
} | ||
|
||
type ProgrammingContent struct{} | ||
|
||
func (c *ProgrammingContent) Play() { | ||
fmt.Println("this is programming content") | ||
} | ||
|
||
type MarketingContent struct{} | ||
|
||
func (c *MarketingContent) Play() { | ||
fmt.Println("this is marketing content") | ||
} | ||
|
||
type ContentType string | ||
|
||
const ( | ||
Bisnis ContentType = "bisnis" | ||
Edukasi ContentType = "edukasi" | ||
) | ||
|
||
type ContentCreator interface { | ||
Produce(time time.Time) Content | ||
Type() ContentType | ||
} | ||
|
||
type Imre struct{} | ||
|
||
func (i *Imre) Produce(time time.Time) Content { | ||
|
||
if time.Weekday().String() == "Tuesday" { | ||
return &CloudContent{} | ||
} else if time.Weekday().String() == "Saturday" { | ||
return &ProgrammingContent{} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i Imre) Type() ContentType { | ||
return Edukasi | ||
} | ||
|
||
type DewaPrayoga struct{} | ||
|
||
func (d *DewaPrayoga) Produce(time time.Time) Content { | ||
return &MarketingContent{} | ||
} | ||
|
||
func (d DewaPrayoga) Type() ContentType { | ||
return Bisnis | ||
} |
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,3 @@ | ||
module github.com/adiputra22/go-design-pattern | ||
|
||
go 1.16 |
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,24 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/adiputra22/go-design-pattern/factory" | ||
) | ||
|
||
func main() { | ||
var contentCreator factory.ContentCreator | ||
|
||
// call imre | ||
contentCreator = &factory.Imre{} | ||
content := contentCreator.Produce(time.Now()) | ||
content.Play() | ||
fmt.Println(contentCreator.Type()) | ||
|
||
// call dewa | ||
contentCreator = &factory.DewaPrayoga{} | ||
content = contentCreator.Produce(time.Now()) | ||
content.Play() | ||
fmt.Println(contentCreator.Type()) | ||
} |