go設計模式之代理模式

代理在計算機領域是個常常被說起的名詞,如nginx就常被用於webf服務的代理。那麼怎麼實現代理這種模式?

package main

import "fmt"

type Subject interface {
   Do() string
}

type RelSubject struct {
}

func (r *RelSubject) Do() string {
   return "test"
}

type Proxy struct {
   real RelSubject
}

func (proxy *Proxy) Do() string {
  res := proxy.real.Do()
  return res
}

func main() {
   var sub Subject
   sub = &Proxy{}

   res := sub.Do()

   fmt.Println(res)
}
複製代碼
相關文章
相關標籤/搜索