lundi 19 juillet 2021

How to composite using-by objects (real world example)

Hey i have question with a real word example. Its related to my two other, but not really answered questions here:

https://softwareengineering.stackexchange.com/questions/423392/no-trivial-god-class-refactoring

and

https://softwareengineering.stackexchange.com/questions/425113/different-composition-techniques-from-the-perspective-of-the-client

Let's say we have a switch with the methods switchOn(), switchOff(). The switch(es) are included in some other structure, for example a switch-bag, from where i can pull out the switches. This can be seen as a ready system.

Now i want to introduce the possibility of switching these switches on after a certain time automatically: A time switch

The time switch ueses now the "normal" switch. The normal switch doesn't have to know something about the time switch.

So but now the client can pull out the normal switch from the switch-bag and now he want also to get to the time-switch related to these normal switch, maybe to configure a new time.

And this is my question, how can the client get to the time-switch?

There are a few possibilites:

  • I refactor the normal switch class to a third-class where the normal-switch and the time switch lives bundled in it. But for this, i break some other client code which still uses the normal switch, but get now from the switch bag a "Combinator"-Class/Object out of it.
  • I don't change anything of the normal switch class. The client if he wants to get access to the time switch have to ask a Map which time switch is related to it. (I think this approach is classical a relation-programming style like a sql relationship database and its not anymore real object-oriented style)
  • I extend the normal switch: Here i also have different options:
    • I change it to a big facade, which delegates the calls to the normal-switch and time switch (its similar to my first solution with the combinator, but here with an facade don't breaking some existing client code)
    • i extend the normal switch letting the existing normal switch code untouched and introduce a component holder. Into this component holder i inject the time switch. So i have these methods there: switchOn(); switchOff(); getComponent("timeSwitch"). But these approach feels like a entity-component system (https://medium.com/ingeniouslysimple/entities-components-and-systems-89c31464240d) but is that still object oriented programming?

i think the last solution is the best one, because its the most flexible.

But what do you think which approach is the best, maybe some approach i didnt mentionend here?

Edit: One more thing you must to know here: The time switch is one extension of the normal switch. One of many. So i want to add of course different more things XYZ switches/behavior extensions to the normal switch

Aucun commentaire:

Enregistrer un commentaire