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

总结

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

相关推荐
前端大白话10 分钟前
Vue2和Vue3语法糖差异大揭秘:一文读懂,开发不纠结!
javascript·vue.js·设计模式
前端大白话16 分钟前
JavaScript中`Symbol.for()`和`Symbol()`的区别,在创建全局唯一的`Symbol`值时如何选择使用?
前端·javascript·设计模式
CHQIUU40 分钟前
Java 设计模式心法之第25篇 - 中介者 (Mediator) - 用“中央协调”降低对象间耦合度
java·设计模式·中介者模式
Pasregret2 小时前
备忘录模式:实现对象状态撤销与恢复的设计模式
运维·服务器·设计模式
碎梦归途3 小时前
23种设计模式-行为型模式之备忘录模式(Java版本)
java·jvm·设计模式·软考·备忘录模式·软件设计师·行为型模式
东阳马生架构12 小时前
Sentinel源码—8.限流算法和设计模式总结二
算法·设计模式·sentinel
冰茶_13 小时前
C#中常见的设计模式
java·开发语言·microsoft·设计模式·微软·c#·命令模式
Niuguangshuo14 小时前
Python 设计模式:访问者模式
python·设计模式·访问者模式
不当菜虚困14 小时前
JAVA设计模式——(七)代理模式
java·设计模式·代理模式
RationalDysaniaer16 小时前
Go设计模式-观察者模式
观察者模式·设计模式·golang