Skip to content

Commit

Permalink
factory design pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
adiputra22 committed Jul 10, 2021
0 parents commit 786a0de
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
67 changes: 67 additions & 0 deletions factory/factory.go
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
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/adiputra22/go-design-pattern

go 1.16
24 changes: 24 additions & 0 deletions main.go
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())
}

0 comments on commit 786a0de

Please sign in to comment.