Published on

Abstract Factory dengan Golang

Authors

Definisi

Abstract Factory Design Pattern adalah salah satu dari creational design pattern yang membantu membuat keluarga dari objek yang berkaitan. ini adalah abstraksi dari factory pattern. Saya akan mengambil contoh kasus sebuah perusahaan Brand Elektronik Apple dan Huawei. Bayangkan jika anda salah satu pengguna device kedua perusahaan tersebut. Masing-masing brand tersebut memiliki produk elektronik:

  • Apple
    1. Macbook (Laptop)
    2. Iphone (Phone)
    3. IWatch (SmartWatch)
  • Huawei
    1. Huawei Matebook (Laptop)
    2. Houawei Honor (Phone)
    3. Huawei GT (SmartWatch)

Nah dari breakdown produk tersebut abstract factory memberikan gambaran tentang produk kongkrit yang akan dibuat oleh perusahaan Huawei dan Apple. Keduanya akan mengimplementasikan IGadgetFactory interfaces. dan kita juga akan membuat 3 produk interfaces.

  • ILaptop interfaces produk ini akan diimplementasi oleh appleLaptop dan huaweiLaptop
  • ISmartWatches interfaces produk ini akan diimplementasi oleh appleWatches dan huaweiWatches
  • ISmartPhone interfaces produk ini akan diimplementasi oleh applePhone dan huaweiPhone

Sekarang mari kita implementasi ke kode program

Implementasi

iGadgetFactory.go
package main

import "fmt"

type iGadgetFactory interface {
	makeLaptop() iLaptop
	makeSmartWatches() iSmartWatches
	makeSmartPhone() iSmartPhone
}

func getGadgetFactory(brand string) (iGadgetFactory, error) {
	if brand == "huawei" {
		return &huawei{}, nil
	}
	if brand == "apple" {
		return &apple{}, nil
	}
	return nil, fmt.Errorf("wrong type")
}


apple.go

package main

type apple struct {
}
type appleLaptop struct {
	laptop
}
type appleSmartPhone struct {
	smartphone
}
type appleSmartWatch struct {
	smartwatches
}

func (a apple) makeLaptop() iLaptop {
	return &appleLaptop{
		laptop{
			screen:    14,
			processor: "M2",
			merk:      "Macbook Pro M2 14 Inci",
		},
	}
}

func (a apple) makeSmartWatches() iSmartWatches {
	return &appleSmartWatch{
		smartwatches{
			screen:    3,
			processor: "Bionic A1",
			merk:      "IWatches",
		},
	}
}

func (a apple) makeSmartPhone() iSmartPhone {
	return &appleSmartPhone{
		smartphone{
			screen:    7,
			processor: "Bionic 15",
			merk:      "iphone 13",
		},
	}
}


huawei.go

package main

type huawei struct {
}
type huaweiLaptop struct {
	laptop
}
type huaweiSmartPhone struct {
	smartphone
}
type huaweiSmartWatch struct {
	smartwatches
}

func (h huawei) makeLaptop() iLaptop {
	return &huaweiLaptop{
		laptop{
			screen:    14,
			processor: "Huawei Processor",
			merk:      "Huawei Matebook Pro",
		},
	}
}

func (h huawei) makeSmartWatches() iSmartWatches {
	return &huaweiSmartWatch{
		smartwatches{
			screen:    4,
			processor: "Huawei Processor",
			merk:      "Huawei GT 3",
		},
	}
}

func (h huawei) makeSmartPhone() iSmartPhone {
	return &huaweiSmartPhone{
		smartphone{
			screen:    7,
			processor: "Huawei Processor",
			merk:      "Huawei Honor Smartphone",
		},
	}
}

iLaptop.go

package main

type iLaptop interface {
	setScreen(screen int)
	setProcessor(processor string)
	setMerk(merk string)
	getScreen() int
	getProcessor() string
	getMerk() string
}
type laptop struct {
	screen    int
	processor string
	merk      string
}

func (l *laptop) setScreen(screen int) {
	l.screen = screen
}
func (l *laptop) setProcessor(processor string) {
	l.processor = processor
}
func (l *laptop) setMerk(merk string) {
	l.merk = merk
}
func (l *laptop) getScreen() int {
	return l.screen
}
func (l *laptop) getProcessor() string {
	return l.processor
}
func (l *laptop) getMerk() string {
	return l.merk
}


ISmartWatches.go

package main

type iSmartWatches interface {
	setScreen(screen int)
	setProcessor(processor string)
	setMerk(merk string)
	getScreen() int
	getProcessor() string
	getMerk() string
}
type smartwatches struct {
	screen    int
	processor string
	merk      string
}

func (l *smartwatches) setScreen(screen int) {
	l.screen = screen
}
func (l *smartwatches) setProcessor(processor string) {
	l.processor = processor
}
func (l *smartwatches) setMerk(merk string) {
	l.merk = merk
}
func (l *smartwatches) getScreen() int {
	return l.screen
}
func (l *smartwatches) getProcessor() string {
	return l.processor
}
func (l *smartwatches) getMerk() string {
	return l.merk
}


ISmartPhone.go

package main

type iSmartPhone interface {
	setScreen(screen int)
	setProcessor(processor string)
	setMerk(merk string)
	getScreen() int
	getProcessor() string
	getMerk() string
}
type smartphone struct {
	screen    int
	processor string
	merk      string
}

func (l *smartphone) setScreen(screen int) {
	l.screen = screen
}
func (l *smartphone) setProcessor(processor string) {
	l.processor = processor
}
func (l *smartphone) setMerk(merk string) {
	l.merk = merk
}
func (l *smartphone) getScreen() int {
	return l.screen
}
func (l *smartphone) getProcessor() string {
	return l.processor
}
func (l *smartphone) getMerk() string {
	return l.merk
}


main.go

package main

import "fmt"

func main() {
	huaweiFactory, _ := getGadgetFactory("huawei")
	appleFactory, _ := getGadgetFactory("apple")
	applePhone := appleFactory.makeSmartPhone()
	appleLaptop := appleFactory.makeLaptop()
	appleSmartWatches := appleFactory.makeSmartWatches()
	huaweiPhone := huaweiFactory.makeSmartPhone()
	huaweiLaptop := huaweiFactory.makeLaptop()
	huaweiSmartWatches := huaweiFactory.makeSmartWatches()
	printPhoneDetails(applePhone)
	printSmartWachesDetails(appleSmartWatches)
	printLaptopDetails(appleLaptop)
	printPhoneDetails(huaweiPhone)
	printSmartWachesDetails(huaweiSmartWatches)
	printLaptopDetails(huaweiLaptop)
}

func printPhoneDetails(s iSmartPhone) {
	fmt.Printf("Screen: %d\n", s.getScreen())
	fmt.Printf("Processor: %s\n", s.getProcessor())
	fmt.Printf("Processor: %s\n", s.getMerk())
}

func printSmartWachesDetails(s iSmartWatches) {
	fmt.Printf("Screen: %d\n", s.getScreen())
	fmt.Printf("Processor: %s\n", s.getProcessor())
	fmt.Printf("Processor: %s\n", s.getMerk())
}
func printLaptopDetails(s iLaptop) {
	fmt.Printf("Screen: %d\n", s.getScreen())
	fmt.Printf("Processor: %s\n", s.getProcessor())
	fmt.Printf("Processor: %s\n", s.getMerk())
}


Output

output-abstract-factory