dimanche 18 décembre 2022

Adaptor pattern in golang

Trying to create adaptor pattern in Golang, Not sure where I am doing wrong. My client.go showing an error
c.broker.placeOrder undefined (type exchange.Exchange has no field or method placeOrder)

main.go

package main

import (
    "context"
    "oms/consumer"
)

func main() {
    ctx := context.Background()
    consumer.Consumer(ctx)
}

consumer.go

package consumer

import (
    "context"
    "fmt"
    "oms/broker"
)

func Consumer(ctx context.Context) {
    broker.Execute()
}

client.go

package broker

import (
    "oms/broker/exchange"
)
type Client struct {
    broker exchange.Exchange
}

func (c *Client) SetBroker(broker exchange.Exchange) {
    c.broker = broker
}

func (c *Client) placeOrder(id string, quantity, price int) {
// I am getting error here
    c.broker.placeOrder(id, quantity, price)
}

broker.go

package broker

// create a Client and set its broker to Paytm
import (
    "oms/broker/paytm"
)

func Execute() {
    client := &Client{}
    client.SetBroker(paytm.Paytm{ /* fields */ })
    client.placeOrder("order1", 10, 100)
}

exchange.go

package exchange

type Exchange interface {
    placeOrder(id string, quantity, price int)
}

paytm.go

package paytm

import "oms/broker/exchange"

type Paytm struct {
    // fields
}

func (p Paytm) placeOrder(id string, quantity, price int) {
    // implementation for Paytm's placeOrder method
}

Aucun commentaire:

Enregistrer un commentaire