https://kotlinlang.org logo
#feed
Title
# feed
x

xxfast

08/18/2022, 4:44 PM

Design Patterns in Kotlin: Proxy and Decorator

Design pattern need not apply? Decorator/Proxy often introduce unnecessary wrapping and types
Copy code
val processor = LoginProcessor(
  Logger,
  CacheProcessor(
    Cache,
    RequestProcessor()
  )
)
We can achieve the same effect without introducing these wrappers/types by simply using extension functions
Copy code
val processor = Processor()
  .log(Logger)
  .cache(Cache)
👍 1
thoughts? @Alexey Soshin
a

Alexey Soshin

08/18/2022, 7:57 PM
Hi @xxfast, Using extension functions is a fine approach as well 🙂
x

xxfast

08/18/2022, 10:55 PM
The pattern really breaks down and we are just left with extension functions 😅 only remaining vestigial is the names decorator and proxy Anyways, great work with the video 👍 really made me rethink this whole pattern for my presentation
p

phldavies

08/19/2022, 11:47 AM
https://pl.kotl.in/t4R0DRmls same idea but with only interfaces and extensions
x

xxfast

08/19/2022, 12:26 PM
Funny how we both came to the same solution without the decorator pattern 😅
p

phldavies

08/19/2022, 12:30 PM
You could argue it’s still the decorate pattern, but without explicit types 😉
x

xxfast

08/19/2022, 2:02 PM
no wrapping though - which is good. This no longer resembles the original pattern from the GoF, therefore it is not decorator
3 Views