samedi 30 janvier 2016

Reusing attributes - Composition over inheritance

From head first design patters (on decorator pattern):

"That is exactly right. If you have code that relies on the concrete component’s type, decorators will break that code. As long as you only write code against the abstract component type, the use of decorators will remain transparent to your code. However, once you start writing code against concrete components, you’ll want to rethink your application design and your use of decorators."

Well, i came along with this problem, i do want to apply inheritance because i have to reuse metods and attributes, but i still need to work based on class/type. Using composition over inheritance works well for methods, but how can i achive the same thing on attributes? I can't make a composition out of that... or do i?

Is it ok, from a design view, to have a class, lets name it "attributesClass" that only contain attributes i want to reuse, and it implements only getters/setters to acces them wherever i want? (not in a DTO motion). Would it be a good idea to use attributeClassInstance.getXAttr() instead of this.XAttr ?

PD: Thanks for your time and, hopefully, you can get accross with my english :P

Aucun commentaire:

Enregistrer un commentaire