美文网首页区块链编程Go
gof23结构类模式(golang版)

gof23结构类模式(golang版)

作者: 五月花号区块链联盟 | 来源:发表于2018-12-02 21:32 被阅读15次

适配器模式

Adapter模式也被称为Wrapper模式,有以下两种:

  1. 类适配器(使用继承)

https://design-

image
实现目标接口,继承被适配类
  1. 对象适配器(使用委托)
image

继承目标类,依赖被适配类

参考http://blog.51cto.com/liuxp08...

package main

import (
    "fmt"
)

func main() {
    duck := &MallardDuck{}
    turkey := &WildTurkey{}

    turkeyAdapter := NewTurkeyAdapter(turkey)

    fmt.Println("The Turkey says...")
    turkey.gobble()
    turkey.fly()

    fmt.Println("The Duck says...")
    duck.quack()
    duck.fly()

    fmt.Println("The Turkey Adapter says...")
    turkeyAdapter.quack()
    turkeyAdapter.fly()
}

type Duck interface {
    quack()
    fly()
}

type Turkey interface {
    gobble()
    fly()
}

type MallardDuck struct {
}

func (*MallardDuck) quack() {
    fmt.Println("Quark...")
}

func (*MallardDuck) fly() {
    fmt.Println("flying...")
}

type WildTurkey struct {
}

func (*WildTurkey) gobble() {
    fmt.Println("Gobble...")
}

func (*WildTurkey) fly() {
    fmt.Println("flying a short distance")
}

type TurkeyAdapter struct {
    turkey Turkey
}

func NewTurkeyAdapter(turkey Turkey) *TurkeyAdapter {
    return &TurkeyAdapter{turkey}
}

func (this *TurkeyAdapter) quack() {
    this.turkey.gobble()
}

func (this *TurkeyAdapter) fly() {
    for i := 0; i < 5; i++ {
        this.turkey.fly()
    }
}

适配器TurkeyAdpater,持有turkey Turkey,实现Duck接口。

代理模式

uml:
https://design-patterns.readt...

image

代理模式中的成员构成:

  • Subject(主体)
  • Proxy (代理人)
  • RealSubject(实际的主体)
  • Client (请求者)

代理的目的是在目标对象方法的基础上作增强,这种增强的本质通常就是对目标对象的方法进行拦截和过滤。

golang版的代理模式如下:

Subject

type Git interface {
    Clone(url string) bool
}

RealSubject

type Github struct{}

func (p Github) Clone(url string) bool {
    if strings.HasPrefix(url, "https") {
        fmt.Println("clone from " + url)
        return true
    }

    fmt.Println("failed to clone from " + url)
    return false
}

Proxy

type GitBash struce {
    //将持有被代理主体
    GitCmd Git
}

func (p GitBash) Clone(url string) bool {
    //实际上是被代理主体在执行动作
    return p.GitCmd.Clone(url)
}

Client
此处client定义为一个coder

type Coder struct {}

func (p Coder) GetCode(url string) {
    gitBash := GetGit()

    if gitBash.Clone(url) {
        fmt.Println("success...")
    } else {
        fmt.Println("failed...")
    }
}

func main() {
    coder := Coder{}

    coder.GetCode("https://www.github.com")
}

装饰模式

uml
https://design-patterns.readt...

image

装饰模式:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式相比生成子类更为灵活。

装饰模式是代理模式的一种特殊应用,两者的共同点是都具有相同的接口,不同点则是代理模式着重为代理过程的控制,而装饰模式则是对类的功能进行加强或减弱,它着重类的功能变化。

package main

import "fmt"

type Noddles interface {
    Description() string
    Price() float32
}

//普通拉面
type Ramen struct {
    name string
    price float32
}

func (p Ramen) Description() string {
    return p.name
}

func (p Ramen) Price() float32 {
    return p.price
}

//想吃鸡蛋拉面怎么办?
type Egg struct {
    //多用组合,少用继承,何况golang没有继承
    noddles Noddles
    name string
    price float32
}

func (p Egg) SetNoddles(noddles Noddles)  {
    p.noddles = noddles
}

func (p Egg) Description() string {
    return p.noddles.Description() + " + " + p.name
}

func (p Egg) Price() float32 {
    return p.noddles.Price() + p.price
}

//加个香肠吧!
type Sausage struct {
    noddles Noddles
    name string
    price float32
}

func (p Sausage) SetNoddles(noddles Noddles) {
    p.noddles = noddles
}

func (p Sausage) Description() string {
    return p.noddles.Description() + " + " + p.name
}

func (p Sausage) Price() float32 {
    return p.noddles.Price() + p.price
}

func main() {
    ramen := Ramen{
        name:"ramen",
        price:8,
    }

    egg := Egg{
        noddles:ramen,
        name:"egg",
        price:2,
    }

    egg2 := Egg{
        noddles:egg,
        name:"egg",
        price:2,
    }

    sausage := Sausage{
        noddles:egg,
        name:"sausage",
        price:2,
    }

    fmt.Println("客官,您的普通拉面来了。。。")
    fmt.Println(ramen.Description())
    fmt.Println(ramen.Price())

    fmt.Println("客官,您的鸡蛋拉面来了。。。")
    fmt.Println(egg.Description())
    fmt.Println(egg.Price())

    fmt.Println("客官,您的双蛋拉面来了。。。")
    fmt.Println(egg2.Description())
    fmt.Println(egg2.Price())

    fmt.Println(sausage.Description())
    fmt.Println(sausage.Price())
    fmt.Println("客官,您的香肠拉面来了。。。")

}

外观模式

外观模式也叫做门面模式,要求一个子系统的外部与其内部的通信必须通过一个统一的对象进行。门面模式提供一个高层次的接口,使得子系统更易于使用。

uml图
https://design-patterns.readt...

image
image

一个服务有若干个子服务,这种情况下,可以引入一个统一的gateway层作为外观模式,统一管理入口。


image
package main

import "fmt"

type Facade struct {
    M Music
    V Video
    C Count
}

func (this *Facade) GetRecommandVideos() error {
    this.V.GetVideos()
    return nil
}

type Video struct {
    vid int64
}

func (this *Video) GetVideos() error {
    fmt.Println("get videos")
    return nil
}

type Music struct {
}

func (this *Music) GetMusic() error {
    fmt.Println("get music material")
    return nil
}

type Count struct {
    PraiseCnt int64
    CommentCnt int64
    CollectCnt int64
}

func (this *Count) GetCountById(id int64) (*Count, error) {
    fmt.Println("get video counts")
    return this, nil
}

func main() {
    f := &Facade{}
    f.GetRecommandVideos()
}

享元模式

image
package main

import (
    "fmt"
    "reflect"
)

type Coordinate struct {
    x, y int
}

type ChessFlyWeight interface {
    getColor() string
    display(c Coordinate)
}

type ConcreteChess struct {
    Color string
}

func (chess ConcreteChess) display(c Coordinate) {
    fmt.Printf("棋子颜色:%s\n", chess.Color)
    fmt.Printf("棋子位置:%d----%d\n", c.x, c.y)
}

func (chess ConcreteChess) getColor() string {
    return chess.Color
}

type ChessFlyWeightFactory struct {
    pool map[string]ChessFlyWeight
}

func (factory ChessFlyWeightFactory) getChess(color string) ChessFlyWeight {
    chess := factory.pool[color];

    if chess == nil {
        chess = ConcreteChess{color}
        factory.pool[color] = chess
    }

    return chess
}

func main() {
    pool  := make(map[string]ChessFlyWeight)

    factory := ChessFlyWeightFactory{pool   }

    chess1 := factory.getChess("black")
    chess2 := factory.getChess("black")

    fmt.Println(reflect.DeepEqual(chess1, chess2))
    c1 := Coordinate{1,2}
    c2 := Coordinate{2, 4}
    chess1.display(c1)
    chess1.display(c2)
}

桥梁模式

image
package main

import (
    "net/http"
    "fmt"
)

//请求接口
type Request interface {
    HttpRequest() (*http.Request, error)
}

type Client struct {
    Client *http.Client
}

func (c *Client) Query(req Request) (resp *http.Response, err error) {
    httpReq, _ := req.HttpRequest()
    resp, err = c.Client.Do(httpReq)
    return
}

//实现
type CdnRequest struct {
}

func (cdn *CdnRequest) HttpRequest() (*http.Request, error) {
    return http.NewRequest("GET", "/cdn", nil)
}

//实现2
type LiveRequest struct {
}

func (cdn *LiveRequest) HttpRequest() (*http.Request, error) {
    return http.NewRequest("GET", "/live", nil)
}

func TestBridge()  {
    client := &Client{http.DefaultClient}

    cdnReq := &CdnRequest{}
    fmt.Println(client.Query(cdnReq))

    liveReq := &LiveRequest{}
    fmt.Println(client.Query(liveReq))
}

相关文章

  • gof23结构类模式(golang版)

    适配器模式 Adapter模式也被称为Wrapper模式,有以下两种: 类适配器(使用继承) https://de...

  • gof23创建类模式(golang版)

    区块链的征程已开启 单例模式 Java中的单例模式的实现可以有饿汉式、懒汉式、双锁、静态内部类、枚举等形式,在go...

  • gof23行为类模式(golang版)

    命令模式 命令模式是一个高内聚的模式,其定义:将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请...

  • 10.9-全栈Java笔记:装饰器模式构建IO流体系

    装饰器模式 装饰器模式是GOF23种设计模式中较为常用的一种模式。它可以实现对原有类的包装和装饰,使新的类具有更强...

  • 10.9-全栈Java笔记:装饰器模式构建IO流体系

    装饰器模式 装饰器模式是GOF23种设计模式中较为常用的一种模式。它可以实现对原有类的包装和装饰,使新的类具有更强...

  • 结构型开发模式-适配器模式

    结构型模式概述 结构型模式分为 类结构模式 和 对象结构模式 类结构模式只关心类的组合,有多个类可以组成一个...

  • 设计模式| 结构型模式

    GOF23种设计模式中结构型模式,共七种: 其他同系列的文章还有:面向对象编程中的六大原则设计模式| 创建型模式设...

  • 15 Golang结构体详解(一)

    Golang中没有“类”的概念,Golang中的结构体struct和其他语言中的类有点相似。和其他面向对象语言中的...

  • Go核心编程-面向对象 [OOP]

    Golang也是支持面向对象(OOP)编程特性的语言,但是Golang中没有类(class),而Go语言的结构体(...

  • 结构型模式

    简介 将类或对象按某种布局组成更大的结构。它分为类结构型模式和对象结构型模式。结构型模式分为:代理模式、适配器模式...

网友评论

    本文标题:gof23结构类模式(golang版)

    本文链接:https://www.haomeiwen.com/subject/oepscqtx.html