implemented strategy pattern
This commit is contained in:
		
							
								
								
									
										34
									
								
								behavioral/strategy/strategy.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								behavioral/strategy/strategy.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
package strategy
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Abstraction
 | 
			
		||||
type LoggerStrategy interface {
 | 
			
		||||
	Write(string) error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Strategy 1
 | 
			
		||||
type FileLogger struct {}
 | 
			
		||||
func (l FileLogger) Write(data string) error {
 | 
			
		||||
	fmt.Println("Writed to file...")
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Strategy 2
 | 
			
		||||
type DbLogger struct {}
 | 
			
		||||
func (l DbLogger) Write(data string) error {
 | 
			
		||||
	fmt.Println("Writed to db..")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Consumer
 | 
			
		||||
type Logger struct{
 | 
			
		||||
	logger LoggerStrategy
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (l *Logger) Log(data string) error {
 | 
			
		||||
	l.logger.Write(data)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										0
									
								
								behavioral/strategy/strategy_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								behavioral/strategy/strategy_test.go
									
									
									
									
									
										Normal file
									
								
							
		Reference in New Issue
	
	Block a user