Golang设计模式(工厂方法模式)

栏目: Go · 发布时间: 5年前

内容简介:工厂方法模式类图,abstract_factory_test.gocar_factory.go

工厂方法模式类图,

Golang设计模式(工厂方法模式)

image.png

abstract_factory_test.go

// abstract_factory_test
package abstract_factory

import (
    "testing"
)

func TestMotorbikeFactory(t *testing.T) {
    motobikeF, err := GetVehicleFactory(MotobikeFactoryType)
    if err != nil {
        t.Fatal("Create motorbike vehicle factory failed")
    }

    vehicle, err := motobikeF.GetVehicle(SportMotorbikeType)
    if err != nil {
        t.Fatal("Create sports motor bike failed")
    }

    sportVehicle, ok := vehicle.(Motorbike)

    if !ok {
        t.Fatal("vehicle is not a motorbike")
    }

    t.Logf("Motorbike type is %d", sportVehicle.GetType())

    _, err = motobikeF.GetVehicle(3)
    if err == nil {
        t.Fatalf("There is no motor bike type is %d", 3)
    }

}

car_factory.go

// car_factory
package abstract_factory

import (
    "errors"
    "fmt"
)

const (
    LuxuryCarType   = 1
    FamiliarCarType = 2
)

type CarFactory struct{}

func (c *CarFactory) GetVehicle(v int) (Vehicle, error) {
    switch v {
    case LuxuryCarType:
        return new(LuxuryCar), nil
    case FamiliarCarType:
        return new(FamiliarCar), nil
    default:
        return nil, errors.New(fmt.Sprintf("Car of type %v not recognized", v))
    }
}

motorbike_factory.go

// motorbike_factory
package abstract_factory

import (
    "errors"
    "fmt"
)

const (
    SportMotorbikeType  = 1
    CruiseMotorbikeType = 2
)

type MotobikeFactory struct{}

func (m *MotobikeFactory) GetVehicle(v int) (Vehicle, error) {
    switch v {
    case SportMotorbikeType:
        return new(SportMotorbike), nil
    case CruiseMotorbikeType:
        return new(CruiseMotorbike), nil
    default:
        return nil, errors.New(fmt.Sprintf("type of motorbike %v not recognized", v))
    }
}

vehicle.go

// vehicle
package abstract_factory

type Vehicle interface {
    GetWheels() int
    GetSeats() int
}

car.go

// car
package abstract_factory

type Car interface {
    GetDoors() int
}

motorbike.go

// motorbike
package abstract_factory

type Motorbike interface {
    GetType() int
}

luxury_car.go

// luxury_car
package abstract_factory

type LuxuryCar struct{}

func (l *LuxuryCar) GetDoors() int {
    return 4
}

func (l *LuxuryCar) GetWheels() int {
    return 4
}

func (l *LuxuryCar) GetSeats() int {
    return 5
}

familiar_car.go

// familiar_car
package abstract_factory

type FamiliarCar struct{}

func (f *FamiliarCar) GetDoors() int {
    return 5
}

func (f *FamiliarCar) GetWheels() int {
    return 4
}

func (f *FamiliarCar) GetSeats() int {
    return 5
}

sport_motorbike.go

// sport_motorbike
package abstract_factory

type SportMotorbike struct{}

func (s *SportMotorbike) GetType() int {
    return SportMotorbikeType
}

func (s *SportMotorbike) GetWheels() int {
    return 2
}
func (s *SportMotorbike) GetSeats() int {
    return 2
}

cruise_motorbike.go

// cruise_motorbike
package abstract_factory

type CruiseMotorbike struct{}

func (c *CruiseMotorbike) GetType() int {
    return CruiseMotorbikeType
}

func (c *CruiseMotorbike) GetWheels() int {
    return 2
}
func (c *CruiseMotorbike) GetSeats() int {
    return 2
}

程序输出如下,单元测试不太充分,不想写了。覆盖率才到25%。

Golang设计模式(工厂方法模式)

image.png


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

叠加体验:用互联网思维设计商业模式

叠加体验:用互联网思维设计商业模式

穆胜 / 机械工业出版社 / 2014-11 / 39.00

本书在互联网思维改变一切的背景下,详细介绍了如何运用互联网思维重构商业模式,主要包括以下内容:①互联网经济中的商业逻辑(即“互联网思维”),不仅给出了消费方面的逻辑变革,还给出了在生产端的逻辑变革以及“跨界”的逻辑变革。②给出了一个“三层产品体验模型”,厘清了互联网思维,打造完美终端、云端服务和价值群落三层体验,企业可以选择做不同层面的体验组合,这即是选择了不同的市场策略。但是,企业要基业长青,终......一起来看看 《叠加体验:用互联网思维设计商业模式》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具