Golang设计模式之代理模式

Jackey Golang 2,449 次浏览 , , 没有评论
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package main
import "fmt"
type Subject interface {
Do() string // 实际业务, 业务系统,检查是否欠费,检查密码是否正确
}
type RealSubject struct {
}
func (sb RealSubject) Do() string {
return "智能执行"
}
type Proxy struct {
real RealSubject
money int
}
func (p Proxy) Do() string {
if p.money > 0 {
return p.real.Do()
} else {
return "请充值"
}
}
func main() {
var sub Subject
sub = &Proxy{money: 0}
fmt.Println(sub.Do())
}
package main import "fmt" type Subject interface { Do() string // 实际业务, 业务系统,检查是否欠费,检查密码是否正确 } type RealSubject struct { } func (sb RealSubject) Do() string { return "智能执行" } type Proxy struct { real RealSubject money int } func (p Proxy) Do() string { if p.money > 0 { return p.real.Do() } else { return "请充值" } } func main() { var sub Subject sub = &Proxy{money: 0} fmt.Println(sub.Do()) }
package main

import "fmt"

type Subject interface {
    Do() string // 实际业务, 业务系统,检查是否欠费,检查密码是否正确
}

type RealSubject struct {
}

func (sb RealSubject) Do() string {
    return "智能执行"
}

type Proxy struct {
    real  RealSubject
    money int
}

func (p Proxy) Do() string {
    if p.money > 0 {
        return p.real.Do()
    } else {
        return "请充值"
    }
}

func main() {
    var sub Subject
    sub = &Proxy{money: 0}
    fmt.Println(sub.Do())
}

 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Go