implemented proxy pattern
This commit is contained in:
		@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user