implemented mediator pattern
This commit is contained in:
		
							
								
								
									
										68
									
								
								behavioral/mediator/mediator.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								behavioral/mediator/mediator.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,68 @@
 | 
			
		||||
package mediator
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"errors"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Mediator
 | 
			
		||||
type TradeService struct {
 | 
			
		||||
	Orders []Order
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (t *TradeService) GetOrders(orderType string) ([]Order, error) {
 | 
			
		||||
	orders := make([]Order, 0)
 | 
			
		||||
 | 
			
		||||
	if orderType != "buy" || orderType != "sell" {
 | 
			
		||||
		return orders, errors.New("Invalid order type provided.")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, v := range t.Orders {
 | 
			
		||||
		if v.OrderType == orderType {
 | 
			
		||||
			orders = append(orders, v)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return orders, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (t *TradeService) PushOrder(o Order)  {
 | 
			
		||||
	// We dont need too much logic to show the purpose of the pattern
 | 
			
		||||
	t.Orders = append(t.Orders, o)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Order struct {
 | 
			
		||||
	OrderType string
 | 
			
		||||
	Price float32
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Trader struct {
 | 
			
		||||
	Service *TradeService
 | 
			
		||||
	OrderToPlace Order
 | 
			
		||||
	InterestedOrders []Order
 | 
			
		||||
}
 | 
			
		||||
func (t *Trader) PlaceOrder()  {
 | 
			
		||||
	t.Service.PushOrder(t.OrderToPlace)
 | 
			
		||||
}
 | 
			
		||||
func (t *Trader) GetOrders(orderType string)  {
 | 
			
		||||
	t.InterestedOrders, _ = t.Service.GetOrders(orderType)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Demo ()  {
 | 
			
		||||
	tradeService := &TradeService{}
 | 
			
		||||
	tradeService.Orders = make([]Order, 0)
 | 
			
		||||
 | 
			
		||||
	traderOne := &Trader{ Service: tradeService}
 | 
			
		||||
	traderOne.OrderToPlace = Order{ "buy", 1.2 }
 | 
			
		||||
	traderOne.PlaceOrder()
 | 
			
		||||
 | 
			
		||||
	traderTwo := &Trader{ Service: tradeService}
 | 
			
		||||
	traderTwo.OrderToPlace = Order{ "sell", 1.5 }
 | 
			
		||||
	traderOne.PlaceOrder()
 | 
			
		||||
 | 
			
		||||
	traderOne.GetOrders("sell")
 | 
			
		||||
	fmt.Println(traderOne.InterestedOrders)
 | 
			
		||||
 | 
			
		||||
	traderTwo.GetOrders("buy")
 | 
			
		||||
	fmt.Println(traderTwo.InterestedOrders)
 | 
			
		||||
}
 | 
			
		||||
@@ -4,6 +4,7 @@ import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Abstractions ...
 | 
			
		||||
type Visitor interface {
 | 
			
		||||
	VisitProductWithHighTask(ProductWithHighTax) float32
 | 
			
		||||
	VisitProductWithLowTask(ProductWithLowTax) float32
 | 
			
		||||
@@ -13,18 +14,18 @@ type Visitable interface {
 | 
			
		||||
	Accept(Visitor) float32
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Visitor
 | 
			
		||||
type TaxVisitor struct {}
 | 
			
		||||
func (v TaxVisitor) VisitProductWithHighTask(p ProductWithHighTax) float32 {
 | 
			
		||||
	fmt.Println("Visiting Product With High Tax ...")
 | 
			
		||||
	return p.Price * 1.3
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (v TaxVisitor) VisitProductWithLowTask(p ProductWithLowTax) float32 {
 | 
			
		||||
	fmt.Println("Visiting Product With Low Tax ...")
 | 
			
		||||
	return p.Price * 1.1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// Visitable objects...
 | 
			
		||||
type ProductWithHighTax struct {
 | 
			
		||||
	Price float32
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user