Which is the name of this Design Pattern | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Which is the name of this Design Pattern

Hi. Previously thought that the Decorator Pattern consisted in attaching something (method | another instance | field,..) to an existing instance. But it ended up being a wrapper that adds functionality, extending without modifying the original. Then, what is the name of the pattern that I confused it with?

7th Jul 2022, 12:28 PM
Pity
8 Answers
+ 2
Tibor Santa Ausgrindtube have found a design pattern that looks pretty similar to what I was referring to: Extension Object
27th Jul 2022, 4:21 AM
Pity
+ 4
Not sure if you find here what you're looking for? https://youtu.be/tv-_1er1mWI We need design patterns mostly because changing existing code is difficult and costly. These patterns are "best practices" applied by developers in the real world, that minimize these costs on the long run, if the code needs to be changed. Modifying an existing instance is not a pattern, because this is what we want to avoid... In my opinion.
7th Jul 2022, 4:48 PM
Tibor Santa
Tibor Santa - avatar
+ 2
I'm not sure that anybody quite knows what you're writing about. Do you have an example that you can attach here? Some links?
7th Jul 2022, 3:15 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
Most modern languages have some sort of "reflection" capability which is able to inject data, new fields etc into instantiated objects at runtime. This can have however serious performance penalties and security risks... But many frameworks rely on such features, like Spring Boot in Java. https://code.sololearn.com/ca9A17a1A4A1/?ref=app
7th Jul 2022, 6:24 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Pity I think the concept of trait, is described in most OOP languages as 'interfaces' or 'mixins', ultimately this problem is solved by inheritance. In Python there is no separate syntactic element for interfaces, but there is multiclass inheritance. https://code.sololearn.com/caRd55t9fbHC/?ref=app https://code.sololearn.com/cChLyLsTJacs/?ref=app
7th Jul 2022, 6:32 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Also I think aspect oriented programming (AOP) deals with similar issue, but I have no direct experience with it. https://en.m.wikipedia.org/wiki/Aspect-oriented_programming
7th Jul 2022, 6:37 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Tibor Santa patterns also serve for good design to avoid huge refactoring due to coupling and so. Ausgrindtube what i describe is like what a trait is in Rust lang basically have the ability to attach functions and variables to an instance. But have a dynamic data structure of pointers as instance member to hold such can be non-ideal, i guess. (but I assume that compilers of languages that support such thigs do it internally) Not sure if now makes more sense.
7th Jul 2022, 5:55 PM
Pity
0
Tibor Santa forgot provide some info was concerned mainly to attach stuff at run-time (to avoid waste memory if not needed) so inheritance has some restriction here. (my concern C# & Rust)
7th Jul 2022, 10:27 PM
Pity