mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-22 03:46:03 +03:00
update factory method & add images
This commit is contained in:
parent
2d05cfd4ce
commit
135283eaa8
@ -2,4 +2,6 @@
|
||||
|
||||
工厂方法模式使用子类的方式延迟生成对象到子类中实现。
|
||||
|
||||
Go中不存在继承 所以使用匿名组合来实现
|
||||
不同的工厂对象,返回一个实现了某接口的类型对象,不同的工厂返回不同实现了相同接口的不同对象,在只能使用特定的类简单工厂的基础上,使用的代码和类生成的灵活性大大增加。
|
||||
|
||||
Go中的继承关系是使用匿名组合来实现的。
|
||||
|
@ -1,66 +1,77 @@
|
||||
package factorymethod
|
||||
|
||||
//Operator 是被封装的实际类接口
|
||||
type Operator interface {
|
||||
SetA(int)
|
||||
SetB(int)
|
||||
Result() int
|
||||
import "fmt"
|
||||
|
||||
//Assistant 是robot能做的事情
|
||||
type Assistant interface {
|
||||
Clean(int)
|
||||
Speak(string)
|
||||
Work() string
|
||||
}
|
||||
|
||||
//OperatorFactory 是工厂接口
|
||||
type OperatorFactory interface {
|
||||
Create() Operator
|
||||
//IRobotFactory must be implemented by Factory
|
||||
//different Factory create different robot
|
||||
type IRobotFactory interface {
|
||||
Build() Assistant
|
||||
}
|
||||
|
||||
//OperatorBase 是Operator 接口实现的基类,封装公用方法
|
||||
type OperatorBase struct {
|
||||
a, b int
|
||||
//BasicRobotModel 是基本的机器人模型
|
||||
type BasicRobotModel struct {
|
||||
words string
|
||||
a, b int
|
||||
}
|
||||
|
||||
//SetA 设置 A
|
||||
func (o *OperatorBase) SetA(a int) {
|
||||
o.a = a
|
||||
//Clean 打扫
|
||||
func (o *BasicRobotModel) Clean(a int) {
|
||||
fmt.Printf("%d", a)
|
||||
}
|
||||
|
||||
//SetB 设置 B
|
||||
func (o *OperatorBase) SetB(b int) {
|
||||
//Speak 说话
|
||||
func (o *BasicRobotModel) Speak(b int) {
|
||||
o.b = b
|
||||
}
|
||||
|
||||
//PlusOperatorFactory 是 PlusOperator 的工厂类
|
||||
type PlusOperatorFactory struct{}
|
||||
//Work main work
|
||||
func (o *BasicRobotModel) Work() string {
|
||||
fmt.Sprint("my main work is do somthing")
|
||||
}
|
||||
|
||||
func (PlusOperatorFactory) Create() Operator {
|
||||
//FightingRobotFactory 生产各类军工机器人
|
||||
type FightingRobotFactory struct{}
|
||||
|
||||
//Build a robot from FightingRobotFactory
|
||||
func (FightingRobotFactory) Build() Assistant {
|
||||
return &PlusOperator{
|
||||
OperatorBase: &OperatorBase{},
|
||||
}
|
||||
}
|
||||
|
||||
//PlusOperator Operator 的实际加法实现
|
||||
type PlusOperator struct {
|
||||
*OperatorBase
|
||||
//FightingRobot 实际的战斗机器人
|
||||
type FightingRobot struct {
|
||||
*BasicRobotModel
|
||||
}
|
||||
|
||||
//Result 获取结果
|
||||
func (o PlusOperator) Result() int {
|
||||
func (o FightingRobot) Result() int {
|
||||
return o.a + o.b
|
||||
}
|
||||
|
||||
//MinusOperatorFactory 是 MinusOperator 的工厂类
|
||||
type MinusOperatorFactory struct{}
|
||||
//HomeRobotFactory 生产各类家用机器人
|
||||
type HomeRobotFactory struct{}
|
||||
|
||||
func (MinusOperatorFactory) Create() Operator {
|
||||
//Build a robot from HomeRobotFactory
|
||||
func (HomeRobotFactory) Build() Assistant {
|
||||
return &MinusOperator{
|
||||
OperatorBase: &OperatorBase{},
|
||||
}
|
||||
}
|
||||
|
||||
//MinusOperator Operator 的实际减法实现
|
||||
type MinusOperator struct {
|
||||
//HomeRobot 实际的家用机器人
|
||||
type HomeRobot struct {
|
||||
*OperatorBase
|
||||
}
|
||||
|
||||
//Result 获取结果
|
||||
func (o MinusOperator) Result() int {
|
||||
func (o HomeRobot) Result() int {
|
||||
return o.a - o.b
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package factorymethod
|
||||
|
||||
import "testing"
|
||||
|
||||
func compute(factory OperatorFactory, a, b int) int {
|
||||
func compute(factory IRobotFactory, a, b int) int {
|
||||
op := factory.Create()
|
||||
op.SetA(a)
|
||||
op.SetB(b)
|
||||
|
@ -1,8 +1,13 @@
|
||||
# 抽象工厂模式
|
||||
|
||||
抽象工厂模式用于生成产品族的工厂,所生成的对象是有关联的。
|
||||
抽象工厂模式用于生成具有多产品种类生产能力的的工厂,所生成的对象往往是有关联的。
|
||||
|
||||
如果抽象工厂退化成生成的对象无关联则成为工厂函数模式。
|
||||
如果抽象工厂退化成生成的对象无关联的或者单一的产品种类则成为工厂函数模式。
|
||||
|
||||
|
||||
参考:[对比](https://blog.csdn.net/wyxhd2008/article/details/5597975)
|
||||
|
||||
![对比图片](../../images/abstract-factorys-method.png)
|
||||
|
||||
比如本例子中使用RDB和XML存储订单信息,抽象工厂分别能生成相关的主订单信息和订单详情信息。
|
||||
如果业务逻辑中需要替换使用的时候只需要改动工厂函数相关的类就能替换使用不同的存储方式了。
|
||||
|
BIN
images/abstract-factorys-method.png
Normal file
BIN
images/abstract-factorys-method.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 67 KiB |
Loading…
Reference in New Issue
Block a user