当函数实现接口时,模式名称是什么?

huangapple go评论81阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年12月27日 05:28:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/74924092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定