php editor Youzi is here to answer a common question: "What is the pattern name when a function implements an interface?" In PHP, when a function implements an interface All methods, this mode is called "interface implementation mode" or "interface adapter mode". The interface implementation pattern is a common design pattern that allows programmers to define a set of methods by implementing an interface, thereby achieving code reuse and modularization. Through this model, we can achieve polymorphism of the interface and improve the flexibility and maintainability of the code. Whether in object-oriented programming or functional programming, the interface implementation pattern is an important technical means.
In go, we can create functions that implement interfaces, such as http.handler
interface and concrete type http.handlerfunc
. I created another simple example of this pattern to calculate bonuses 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 the interface bonuscalculator
instead of using a struct to do the same thing.
Does this pattern have a name? I've seen it in many places but never found its name.
Does this pattern have a name? I've seen it in many places but never found its name.
Yes, this pattern is called Adapter because it allows a certain "interface" (in this case, a closure) to be used as another kind of interface (satisfying bonuscalculator
type).
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 you need a type that satisfies bonusfunc
, i.e. has a method calculate(salary float64) float64
(Closures have no methods with such names, so they do not satisfy bonuscalculator
).
What you want is to adapt func(salary float64) float64
to bonuscalculator
. So you define a new type bonusfunc
which is an adapter. It will be derived from the closure type you want to adapt and then satisfy bonuscalculator
by defining a method calculate(salary float64) float64
which simply calls the underlying closure :
type BonusFunc func(salary float64) float64 func (bonus BonusFunc) Calculate(salary float64) float64 { return bonus(salary) }
bonusfunc
is an adapter; it exists only to adapt func(salary float64) float64
in order to satisfy the bonuscalculator
interface. Now, whenever you assign func(salary float64) float64
to bonusfunc
, you will get a value that satisfies bonuscalculator
.
The above is the detailed content of What is the schema name when a function implements an interface?. For more information, please follow other related articles on the PHP Chinese website!