抽象工厂设计模式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.)

总结

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

相关推荐
Villiam_AY10 分钟前
Go 后端中双 token 的实现模板
开发语言·后端·golang
敲代码的 蜡笔小新1 小时前
【行为型之访问者模式】游戏开发实战——Unity灵活数据操作与跨系统交互的架构秘诀
unity·设计模式·c#·访问者模式
Chandler2413 小时前
Go语言 GORM框架 使用指南
开发语言·后端·golang·orm
wktomo15 小时前
GO语言学习(二)
学习·golang
你怎么知道我是队长15 小时前
Go语言语法---输入控制
golang
蚂蚁在飞-15 小时前
Golang基础知识—cond
开发语言·后端·golang
软考真题app15 小时前
软件设计师考试结构型设计模式考点全解析
设计模式·软件设计师·结构型设计模式·考试考点
李迟15 小时前
Golang实践录:在go中使用curl实现https请求
开发语言·golang·https
BUG制造机.16 小时前
Go 语言的 GMP 模型
golang
张帅涛_66616 小时前
golang读、写、复制、创建目录、删除、重命名,文件方法总结
golang