package inital
//redis
type Cache interface {
Set (key string,value string)
Get(key string) string
}
type Redis struct{
RedisValue map[string]string
}
func(this *Redis)Set(key string,value string){
this.RedisValue=make(map[string]string)
this.RedisValue[key]=value
}
func (this *Redis)Get(key string)string{
return this.RedisValue[key]
}
//todo 不过这里面如果是interface 要该怎么做呢
//memCache
type MemCache struct{
RedisValue map[string]string
}
func(this *MemCache)Set(key string,value string){
this.RedisValue=make(map[string]string)
this.RedisValue[key]=value
}
func (this *MemCache)Get(key string)string{
return this.RedisValue[key]
}
type DoSimpleFactoryImplement struct{
}
func (this *DoSimpleFactoryImplement)DoSimpleFactoryImplement(input string)Cache {
switch input {
case "memCache":
return &MemCache{}
case "redis":
return &Redis{}
}
return nil
}
测试代码:
func TestFactoryImplement(t *testing.T){
factory:=inital.DoSimpleFactoryImplement{}
redis:=factory.DoSimpleFactoryImplement("redis")
redis.Set("cart:1001","10")
fmt.Println(redis.Get("cart:1001"))
//______——————————————————————————————————————————————
memCache:=factory.DoSimpleFactoryImplement("memCache")
memCache.Set("cart:1001","1010")
fmt.Println(memCache.Get("cart:1001"))
}