英文:
What is the pattern name when functions implementing interfaces?
问题
在Go语言中,我们可以创建实现接口的函数,比如http.Handler
接口和具体类型http.HandlerFunc
。我创建了另一个简单的例子来演示这种模式,用于计算不同员工的奖金。
type BonusCalculator interface {
Calculate(salary float64) float64
}
type BonusFunc func(salary float64) float64
func (bonus BonusFunc) Calculate(salary float64) float64 {
return bonus(salary)
}
var (
DeveloperBonus BonusFunc = func(salary float64) float64 { return salary*1.5 + 2500.00 }
ManagerBonus BonusFunc = func(salary float64) float64 { return salary * 2.0 }
DirectorBonus BonusFunc = func(salary float64) float64 { return salary * 3.3 }
)
func CalculateBonus(bonus BonusCalculator, salary float64) float64 {
return bonus.Calculate(salary)
}
func main() {
bonus := CalculateBonus(DirectorBonus, 35000.00)
fmt.Printf("bonus %.2f", bonus)
}
所以上面的代码中,我们使用了简单的BonusFuncs
来实现BonusCalculator
接口,而不是使用结构体来完成相同的功能。
这种模式有一个名称吗?我在很多地方看到它,但从未找到一个名称。
英文:
In Go we can create functions that implement an interface, like http.Handler
interface and concrete type http.HandlerFunc
. I created another simple example of this kind of pattern to calculate bonus for different employees.
type BonusCalculator interface {
Calculate(salary float64) float64
}
type BonusFunc func(salary float64) float64
func (bonus BonusFunc) Calculate(salary float64) float64 {
return bonus(salary)
}
var (
DeveloperBonus BonusFunc = func(salary float64) float64 { return salary*1.5 + 2500.00 }
ManagerBonus BonusFunc = func(salary float64) float64 { return salary * 2.0 }
DirectorBonus BonusFunc = func(salary float64) float64 { return salary * 3.3 }
)
func CalculateBonus (bonus BonusCalculator, salary float64) float64 {
return bonus.Calculate(salary)
}
func main() {
bonus := CalculateBonus(DirectorBonus, 35000.00)
fmt.Printf("bonus %.2f", bonus)
}
So above we have simple BonusFuncs
implementing interface BonusCalculator
instead of use structs to do the same.
Is there a name to this pattern? I see it in many places and never found a name for it.
答案1
得分: 4
这个模式被称为适配器模式(Adapter),因为它允许一种类型的"接口"(在这种情况下是闭包)被用作另一种类型的接口(满足BonusCalculator
的类型)。
在你的例子中,你有一个接口BonusCalculator
:
type BonusCalculator interface {
Calculate(salary float64) float64
}
然而,你有一些类型为func(salary float64) float64
的闭包,并且你希望能够在需要满足BonusCalculator
的类型时传递它们(闭包没有一个名为Calculate
的方法,所以它们不满足BonusCalculator
)。
你想要的是将func(salary float64) float64
适配为BonusCalculator
。因此,你定义了一个新类型BonusFunc
,它是适配器。它将派生自你想要适配的闭包类型,并且通过在其上定义一个Calculate(salary float64) float64
方法来满足BonusCalculator
,该方法只是调用底层的闭包:
type BonusFunc func(salary float64) float64
func (bonus BonusFunc) Calculate(salary float64) float64 {
return bonus(salary)
}
BonusFunc
就是适配器;它只存在于将func(salary float64) float64
适配为满足BonusCalculator
的接口时。现在,每当你将func(salary float64) float64
赋值给BonusFunc
时,你将得到一个满足BonusCalculator
的值。
英文:
> Is there a name to this pattern? I see it in many places and never found a name for it.
Yes, this pattern is called Adapter because it allows some sort of "interface" (closures in this case) to be used as another kind of interface (a type that satisfies BonusCalculator
).
In your example, you have an interface BonusCalculator
:
type BonusCalculator interface {
Calculate(salary float64) float64
}
However, you have closures of type func(salary float64) float64
, and you want to be able to pass them when a type that satisfies BonusFunc
is required, i.e., a type that has the method Calculate(salary float64) float64
(the closures don't have a method with such name, so they don't satisfy BonusCalculator
).
What you want is to adapt a func(salary float64) float64
to a BonusCalculator
. So, you define a new type, BonusFunc
, which is the adapter. It will derive from the closure type you want to adapt and, in turn, will satisfy BonusCalculator
by defining a method Calculate(salary float64) float64
on it that simply calls the underlying closure:
type BonusFunc func(salary float64) float64
func (bonus BonusFunc) Calculate(salary float64) float64 {
return bonus(salary)
}
The BonusFunc
is the Adapter; it exists only for adapting a func(salary float64) float64
so that the interface BonusCalculator
can be satisfied. Now, whenever you assign a func(salary float64) float64
to a BonusFunc
, you obtain a value that satisfies Bonuscalculator
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论