抽象工厂设计模式go实现尝试

文章目录


前言

本文章尝试使用go实现"抽象工厂"。


代码

go 复制代码
package main

import (
	"fmt"
)

// 所有系列产品A接口
type IAbstractProductA interface {
	UsefulFunctionA() string
}

// 系列1具体产品A
type ConcreteProductA1 struct {
}

func (ConcreteProductA1) UsefulFunctionA() string {
	return "The result of the product A1."
}

// 系列2具体产品A
type ConcreteProductA2 struct {
}

func (ConcreteProductA2) UsefulFunctionA() string {
	return "The result of the product A2."
}

// 所有系列产品B接口
type IAbstractProductB interface {
	UsefulFunctionB() string
	AnotherUsefulFunctionB(collaborator IAbstractProductA) string
}

// 系列1具体产品B
type ConcreteProductB1 struct {
}

func (ConcreteProductB1) UsefulFunctionB() string {
	return "The result of the product B1."
}

func (ConcreteProductB1) AnotherUsefulFunctionB(collaborator IAbstractProductA) string {
	result := collaborator.UsefulFunctionA()
	return fmt.Sprintf("The result of the B1 collaborating with the (%s)", result)
}

// 系列2具体产品B
type ConcreteProductB2 struct {
}

func (ConcreteProductB2) UsefulFunctionB() string {
	return "The result of the product B2."
}

func (ConcreteProductB2) AnotherUsefulFunctionB(collaborator IAbstractProductA) string {
	result := collaborator.UsefulFunctionA()
	return fmt.Sprintf("The result of the B2 collaborating with the (%s)", result)
}

// 抽象工厂接口定义一系列方法返回同个系列的若干抽象产品
type IAbstractFactory interface {
	CreateProductA() IAbstractProductA
	CreateProductB() IAbstractProductB
}

// 具体工厂1
type ConcreteFactory1 struct {
}

func (ConcreteFactory1) CreateProductA() IAbstractProductA {
	return &ConcreteProductA1{}
}

func (ConcreteFactory1) CreateProductB() IAbstractProductB {
	return &ConcreteProductB1{}
}

// 具体工厂2
type ConcreteFactory2 struct {
}

func (ConcreteFactory2) CreateProductA() IAbstractProductA {
	return &ConcreteProductA2{}
}

func (ConcreteFactory2) CreateProductB() IAbstractProductB {
	return &ConcreteProductB2{}
}

// 客户端代码
func clientCode(factory IAbstractFactory) {
	product_a := factory.CreateProductA()
	product_b := factory.CreateProductB()
	fmt.Println(product_b.UsefulFunctionB())
	fmt.Println(product_b.AnotherUsefulFunctionB(product_a))
}

func main() {
	fmt.Println("Client: Testing client code with the first factory type:")
	clientCode(&ConcreteFactory1{})
	fmt.Println()
	fmt.Println("Client: Testing the same client code with the second factory type:")
	clientCode(&ConcreteFactory2{})
}

结果

复制代码
Client: Testing client code with the first factory type:
The result of the product B1.
The result of the B1 collaborating with the (The result of the product A1.)

Client: Testing the same client code with the second factory type:
The result of the product B2.
The result of the B2 collaborating with the (The result of the product A2.)

总结

新人设计模式理解,望大家多多指点。

相关推荐
电子科技圈1 小时前
芯科科技推出智能开发工具Simplicity Ecosystem软件开发套件开启物联网开发的新高度
mcu·物联网·设计模式·软件工程·软件构建·iot·1024程序员节
WaWaJie_Ngen2 小时前
【设计模式】组合模式(Composite)
设计模式·组合模式·1024程序员节
WaWaJie_Ngen4 小时前
【设计模式】装饰器模式(Decorator)
设计模式·装饰器模式
YuanlongWang4 小时前
C# 设计模式——观察者
windows·设计模式·c#
亿牛云爬虫专家4 小时前
中间件实现任务去重与精细化分发:设计模式与常见陷阱
设计模式·中间件·爬虫代理·数据抓取·商品信息·数据去重·电商搜索
Yeats_Liao14 小时前
Go Web 编程快速入门 05 - 表单处理:urlencoded 与 multipart
前端·golang·iphone
Tony Bai15 小时前
【Go 网络编程全解】12 本地高速公路:Unix 域套接字与网络设备信息
开发语言·网络·后端·golang·unix
消失的旧时光-194316 小时前
kmp需要技能
android·设计模式·kotlin
Yeats_Liao16 小时前
Go Web 编程快速入门 06 - 响应 ResponseWriter:状态码与头部
开发语言·后端·golang
mit6.82416 小时前
[Agent可视化] 编排工作流(Go) | Temporal引擎 | DAG调度器 | ReAct模式实现
开发语言·后端·golang