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

总结

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

相关推荐
一叶飘零_sweeeet5 小时前
深度拆解汽车制造系统设计:用 Java + 设计模式打造高扩展性品牌 - 车型动态生成架构
java·设计模式·工厂设计模式
阿波罗尼亚6 小时前
设计原则(一)Head First设计模式
设计模式
小画家~9 小时前
第二十八:golang Time.time 时间格式返回定义结构体
java·前端·golang
q***756013 小时前
【Golang】——Gin 框架中间件详解:从基础到实战
中间件·golang·gin
资深web全栈开发19 小时前
力扣2536子矩阵元素加1-差分数组解法详解
算法·leetcode·矩阵·golang·差分数组
ZHE|张恒19 小时前
设计模式实战篇(五):责任链模式 — 把复杂审批/过滤流程变成可组合的“传递链”
设计模式·责任链模式
CodeAmaz19 小时前
使用责任链模式设计电商下单流程(Java 实战)
java·后端·设计模式·责任链模式·下单
大G的笔记本1 天前
Java常见设计模式面试题(高频)
java·开发语言·设计模式
老鼠只爱大米1 天前
Java设计模式之建造者模式(Builder)详解
java·设计模式·建造者模式·builder·23种设计模式
guangzan1 天前
常用设计模式:职责链模式
设计模式