You don't need to change the original function. Below, Fib(int) int is
the original function, and it illustrates two ways to decorate it.

https://play.golang.org/p/K6us_rHIsyM

Neither is a *bad* way to write code. Go is a different language so
some things you're used to in other languages must be done in
different ways. This sometimes requires you to re-engineer your
solution. So trying to replicate a Java decorator pattern
implementation in Go using Java features won't work. Change the way
you're trying to solve your problem.

You use decorators to make existing code fit another scenario. For
this to work, the receiving end of the problem must be able to handle
such decoration as well. If you have code that uses the function `Fib`
directly, then you cannot override Fib. You cannot override Fib in
Java either (don't know about python). Your code should be able to get
an indirection of Fib (either an interface or a function pointer) so
you can override it.


On Sat, Nov 2, 2019 at 4:34 AM 林风 <petelin1...@gmail.com> wrote:
>
> there is a way i can do the base, but we still change old function due limit 
> on go
>
> https://gist.github.com/Petelin/a7bf8ccfd657536d46aaa355c6dc421a
>
>
> 在 2019年11月2日星期六 UTC+8下午4:50:22,林风写道:
>>
>> you may say i can write a fib2 which have fib1+counter. but that is really 
>> bad way to write code. and that's why we need decorator.
>>
>> 在 2019年11月2日星期六 UTC+8下午2:30:33,burak serdar写道:
>>>
>>> On Sat, Nov 2, 2019 at 12:20 AM 林风 <petel...@gmail.com> wrote:
>>> >
>>> > let's say i have a function or method to get fib
>>> >
>>> > func fib(n int){
>>> >   ...
>>> >   return fib(n-1) + fib(n-2)
>>> > }
>>> >
>>> >
>>> > now i want to add a counter to track how many times i have called to fib.
>>> >
>>> > in python or jave i can easy use `decorator patten` to do it.
>>> >
>>> > but in go, you can not change fib since it is a function. if you define 
>>> > fib as a method, you cannot do it either since go did not have 
>>> > `Inherited`. you cannot call father class.
>>> >
>>> > so golang did not support such thing? can we just reflect to do so?
>>>
>>>
>>> There is no built-in way to do that. I don't think you can do that
>>> with reflect either. However, if you need such decorators, you can use
>>> an interface or a function pointer:
>>>
>>> type fib interface {
>>>    fib(int) int
>>> }
>>>
>>> func f(in fib) {
>>>    in.fib(...)
>>> }
>>>
>>> You can pass in an implementation of fib that will do whatever you want.
>>>
>>> With a function pointer:
>>>
>>> func f(fib func(int) int) {
>>>    fib(...)
>>> }
>>>
>>> And if you need to count how many times f calls fib:
>>>
>>> ...
>>> count:=0
>>> f(func(in int) int {
>>>    count++
>>>    return originalFib(in)
>>>   }
>>> }
>>>
>>>
>>>
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> > --
>>> > You received this message because you are subscribed to the Google Groups 
>>> > "golang-nuts" group.
>>> > To unsubscribe from this group and stop receiving emails from it, send an 
>>> > email to golan...@googlegroups.com.
>>> > To view this discussion on the web visit 
>>> > https://groups.google.com/d/msgid/golang-nuts/84685fb5-8657-4f57-94f1-4b444d722976%40googlegroups.com.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/ad4658a5-c6a6-4cfb-ad48-95ce8ecbfa2b%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAMV2Rqr5uzTL8FKN%3D6NhbZaCrBir27OCMhk-1i8O5MP4nLDfKQ%40mail.gmail.com.

Reply via email to