N
N
nocach2011-04-16 18:15:21
Software design
nocach, 2011-04-16 18:15:21

Architecture designing classes with different logic in different places?

Hello readers.
The title of the question is a bit confusing, now I will explain in more detail.
There is a Bullet class. It has many different methods associated with different properties of the bullet. Possible fall trajectory, certain hitting logic, certain behavior at a certain point in time, different possible responses to other bullets. And each such property can vary quite a lot from class to class. Unfortunately, I did everything through inheritance, i.e. if the class wanted to change 2 properties, then it inherited from the main one and changed them.
After the growth of the architecture, quite a few varieties of Bullet appeared, and here the problem of this approach became obvious. Let's say MagicBullet inherits from Bullet and changes the hit and explosion logic. There is also a SuperBullet class that inherits from Bullet and changes only the trajectory. And now I needed a class in which I want the hit logic from MagicBullet and the trajectory logic from SuperBullet. There is a problem of code duplication, because Java does not support multiple inheritance.
The question is, how would the architecture look like in this case? Sub-question - how will it be most painless to fix the current architecture?
Regarding the first question, I have 2 options so far:
1. Use strategies for each of the individual properties. It seems that the option is not bad, but it greatly limits the logic by the fact that, in theory, each of the strategies should not know anything about the other strategies, and it often happens that 2 changed properties are in close connection and possible connections may also differ from the case to the occasion.
2. Use one Bullet class and many different decorators. However, again, the question of the relationship between different properties remains.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
Zayaz, 2011-04-16
@nocach

If you want the class's inheritors to be independent, you'll have to make them independent, even breaking DRY.
If you can’t hierarchically select subtypes of the Bullet class, from which you can already make descendants with the necessary properties (i.e., make, for example, ExplosiveBullet with a bullet break and MagicExplosiveBullet, SniperExplosiveBullet, etc - already with specific implementations), then you can’t use Java here nice not to get out.
If you just really need to get away from the current implementation, but get rid of code duplication, you can use the Mixin pattern, like I saw the implementation in Java.

V
VenomBlood, 2011-04-16
@VenomBlood

Separate the specific behavior for each aspect into separate classes (trajectories to one class group, the logic of hitting another, etc.), and in classes that inherit from bullet, simply use the logic from them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question