Design pattern in golang: builder

Builder pattern dipakai untuk membuat sebuah instance dari sebuah struct — beserta dengan semua propertinya.

Perhatikan struct berikut:

package main

type User struct {
name string
email string
address string
dateO…


This content originally appeared on DEV Community and was authored by ynwd

Builder pattern dipakai untuk membuat sebuah instance dari sebuah struct -- beserta dengan semua propertinya.

Perhatikan struct berikut:

package main

type User struct {
    name        string
    email       string
    address     string
    dateOfBirth DateOfBirth
}
package main

type DateOfBirth struct {
    year  int
    month int
    day   int
}

Implementasi pattern

package main

import (
    "fmt"
    "strconv"
    "strings"
)

func newUserBuilder() *User {
    return &User{}
}

func (u *User) Build() (User, error) {
    return User{
        name:        u.name,
        email:       u.email,
        address:     u.address,
        dateOfBirth: u.dateOfBirth,
    }, nil
}

func (u *User) SetName(name string) {
    u.name = name
}

func (u *User) SetEmail(email string) {
    u.email = email
}

func (u *User) SetAddress(address string) {
    u.address = address
}

func (u *User) SetDateOfBirth(dob string) {
    splitted := strings.Split(dob, "/")

    day, err := strconv.Atoi(splitted[0])
    if err != nil {
        u.dateOfBirth = DateOfBirth{}
    }

    month, err := strconv.Atoi(splitted[1])
    if err != nil {
        u.dateOfBirth = DateOfBirth{}
    }

    year, err := strconv.Atoi(splitted[2])
    if err != nil {
        u.dateOfBirth = DateOfBirth{}
    }

    u.dateOfBirth = DateOfBirth{day, month, year}
}

Bahkan, kalau mau, kamu dapat menambahkan validasi pada setiap variabel yang akan menjadi input -- sebelum ia dipakai oleh setiap properti dari sebuah struct.

Cara penggunaan

package main

func main() {
    builder := newUserBuilder()
    builder.SetName("agus")
    builder.SetAddress("cirebon")
    builder.SetEmail("agus@email.com")
    builder.SetDateOfBirth("01/01/2020")
    user, err := builder.Build()
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(user)
}

Kesimpulan

Dengan builder pattern kita dapat membuat instance dari sebuah struct dan menambahkan validasi pada variabel-variabel yang akan menjadi input-nya.

Source code

https://play.golang.org/p/7nBzfPFT8ix


This content originally appeared on DEV Community and was authored by ynwd


Print Share Comment Cite Upload Translate Updates
APA

ynwd | Sciencx (2021-11-14T10:25:42+00:00) Design pattern in golang: builder. Retrieved from https://www.scien.cx/2021/11/14/design-pattern-in-golang-builder/

MLA
" » Design pattern in golang: builder." ynwd | Sciencx - Sunday November 14, 2021, https://www.scien.cx/2021/11/14/design-pattern-in-golang-builder/
HARVARD
ynwd | Sciencx Sunday November 14, 2021 » Design pattern in golang: builder., viewed ,<https://www.scien.cx/2021/11/14/design-pattern-in-golang-builder/>
VANCOUVER
ynwd | Sciencx - » Design pattern in golang: builder. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/14/design-pattern-in-golang-builder/
CHICAGO
" » Design pattern in golang: builder." ynwd | Sciencx - Accessed . https://www.scien.cx/2021/11/14/design-pattern-in-golang-builder/
IEEE
" » Design pattern in golang: builder." ynwd | Sciencx [Online]. Available: https://www.scien.cx/2021/11/14/design-pattern-in-golang-builder/. [Accessed: ]
rf:citation
» Design pattern in golang: builder | ynwd | Sciencx | https://www.scien.cx/2021/11/14/design-pattern-in-golang-builder/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.