![]() |
Decorator [Design Pattern] - Printable Version +- Gothic Online Forums (https://archive.gothic-online.com) +-- Forum: Scripting (English) (https://archive.gothic-online.com/forum-11.html) +--- Forum: Tutorials (https://archive.gothic-online.com/forum-13.html) +--- Thread: Decorator [Design Pattern] (/thread-2676.html) |
Decorator [Design Pattern] - Patrix - 09.09.2019
Decorator
Hi, have you ever heard about the decorator design pattern? If not, just read this topic, and hopefully you'll learn the basic concept of this powerful abstract idea. The basic concept In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern. source: wikipedia.org Practical example Basically the decorator pattern allows us to compose the object from multiple classes which inherits from the Decorator class. Let's just say, that we want to create a simple class called Pizza. We also want to make more types of pizza, to do that we could use the inheritance. Squirrel Script
This silly example demonstrates the basic problem, if we use the inheritance, we will end up with many types of classes, and we can't easilly combine our own pizza object from the ingredients. So, instead of making multiple class inheritance, we will try to use the Decorator pattern in practice. The pattern requires from us to implement the class called Decorator which will be holding a reference to the pizza object, or any object, we will make this class to be universal, that means that it could be used in different scenarios, not only pizza example. Squirrel Script
As you can see, the idea behind the pattern is really simple. The Decorator class holds a reference to the object, using this we can make multiple classes which will inherit from the Decorator and then make the object from the specified classes. This gives you a lot more flexibility, because you can compose the object from many classes that inherits from the Decorator. In case of usage in G2O, think about the NPC AI written with the usage of this pattern, you could compose the AI from small classes which would implement only partial functionallity. The code is based on this video: RE: Decorator [Design Pattern] - Quarchodron - 10.09.2019 Good Job! |