I have a certain hierarchy of game resources. At the top of the hierarchy is the Resource class. It has some data that all subclasses of this class possess. And by itself some methods. Below are the Bag, Transport, Food, Clothes, etc. classes. Each of these classes has its own additional data and methods. I want to apply a pattern decorator to this hierarchy. One of the decorators will self-update the object when changes are made to it (objects are stored in the database), the Logging decorator, etc. The problem is that I don’t know how to apply the decorator pattern to the existing class hierarchy. All the examples of decorators that I saw were not applied to a certain class hierarchy. There simply was a pair of classes with the same set of methods and data. And it turns out that it was required to write one wrapper and it was possible to wrap any of these objects into it. I get that for each object you need to write your own logging, updating, and so on wrapper, since the set of methods and data is different for each object. And what is very important, my class hierarchy should be preserved.

  • decorator only complicates the understanding of your logic. only you will know her. But imagine if you need to use your classes for third-party developers - Sanaev
  • it may be worth extending classes using inheritance (base classes declare methods with logging, update logic, etc.), or write bins / helpers to perform these actions. In general, your question turned out to be too abstract, without concrete classes and methods, it is difficult to suggest anything here - DaysLikeThis

0