设计模式(go)

单例模式:

饿汉:

懒汉:

Go优雅完成

工厂模式:

3类:

简单工厂模式:

package main

import "fmt"

// Shape 接口
type Shape interface {
	Draw()
}

// Circle 结构体
type Circle struct{}

func (c Circle) Draw() {
	fmt.Println("Drawing a Circle")
}

// Rectangle 结构体
type Rectangle struct{}

func (r Rectangle) Draw() {
	fmt.Println("Drawing a Rectangle")
}

// 简单工厂函数
func ShapeFactory(shapeType string) Shape {
	switch shapeType {
	case "circle":
		return Circle{}
	case "rectangle":
		return Rectangle{}
	default:
		return nil
	}
}

func main() {
	circle := ShapeFactory("circle")
	circle.Draw()

	rectangle := ShapeFactory("rectangle")
	rectangle.Draw()
}

工厂方法模式

package main

import "fmt"

// Shape 接口
type Shape interface {
	Draw()
}

// Circle 结构体
type Circle struct{}

func (c Circle) Draw() {
	fmt.Println("Drawing a Circle")
}

// Rectangle 结构体
type Rectangle struct{}

func (r Rectangle) Draw() {
	fmt.Println("Drawing a Rectangle")
}

// ShapeFactory 接口
type ShapeFactory interface {
	CreateShape() Shape
}

// CircleFactory 结构体
type CircleFactory struct{}

func (cf CircleFactory) CreateShape() Shape {
	return Circle{}
}

// RectangleFactory 结构体
type RectangleFactory struct{}

func (rf RectangleFactory) CreateShape() Shape {
	return Rectangle{}
}

func main() {
	var factory ShapeFactory

	factory = CircleFactory{}
	circle := factory.CreateShape()
	circle.Draw() // 输出:Drawing a Circle

	factory = RectangleFactory{}
	rectangle := factory.CreateShape()
	rectangle.Draw() // 输出:Drawing a Rectangle
}

抽象工厂模式

package main

import "fmt"

// Shape 接口
type Shape interface {
	Draw()
}

// Color 接口
type Color interface {
	Fill()
}

// Circle 结构体
type Circle struct{}

func (c Circle) Draw() {
	fmt.Println("Drawing a Circle")
}

type Rectangle struct{}

func (r Rectangle) Draw() {
	fmt.Println("Drawing a Rectangle")
}

// Red 结构体
type Red struct{}

func (r Red) Fill() {
	fmt.Println("Filling with Red color")
}

type Blue struct{}

func (b Blue) Fill() {
	fmt.Println("Filling with Blue color")
}

// AbstractFactory 接口
type AbstractFactory interface {
	CreateShape() Shape
	CreateColor() Color
}

// ShapeFactory 结构体
type ShapeFactory struct {
	shapeType string
}

func (sf ShapeFactory) CreateShape() Shape {
	if sf.shapeType == "circle" {
		return Circle{}
	} else if sf.shapeType == "rectangle" {
		return Rectangle{}
	}
	return nil
}

func (sf ShapeFactory) CreateColor() Color {
	// 此处可以实现颜色的逻辑
	return nil
}

func main() {
	shapeFactory := ShapeFactory{shapeType: "circle"}
	shape := shapeFactory.CreateShape()
	shape.Draw() // 输出:Drawing a Circle

	colorFactory := ShapeFactory{shapeType: "red"}
	color := colorFactory.CreateColor()
	if color != nil {
		color.Fill() // 输出:Filling with Red color (需要实现填充逻辑)
	}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值