From 28814bb7a35cfa73a52367d23b25598451711095 Mon Sep 17 00:00:00 2001 From: Ismayil_Niftaliyev Date: Wed, 17 Jan 2018 16:31:15 +0300 Subject: [PATCH] implemented proxy pattern --- structural/proxy/proxy.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/structural/proxy/proxy.go b/structural/proxy/proxy.go index e69de29..6b7eb86 100644 --- a/structural/proxy/proxy.go +++ b/structural/proxy/proxy.go @@ -0,0 +1,37 @@ +package proxy + +import "errors" + +type Human struct { + Gender string + Age int +} + +type Bar interface { + Welcome(Human) error +} + +type OpenBar struct { + Visitors []Human +} + +func (b *OpenBar) Welcome(h Human) error { + b.Visitors = append(b.Visitors, h) + return nil +} + +type BarProxy struct { + bar Bar +} + +func (b *BarProxy) Welcome(h Human) error { + if h.Gender == "female" && h.Age < 20 { + return errors.New("Females under 20 not allowed.") + } + + if h.Gender == "male" && h.Age < 18 { + return errors.New("Males under 18 not allowed.") + } + + return b.bar.Welcome(h) +}