Skip to content Skip to sidebar Skip to footer

Is Using Onclicklistener() An Example Of Strategy Pattern?

Is OnClickListener in android an example of Strategy pattern ? In another Stackoverflow question accepted answer says it is Observer Pattern. Similar Code to understand the questi

Solution 1:

Here is the intent of the Observer Pattern from page 293.

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Strictly speaking, the code example is not an Observer then, because the one-to-many relationship is rather one-to-one. However, I would not call this a Strategy either, per its intent from page 315.

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Semantically, a listener is not an algorithm; i.e. responding to an event is a different purpose from performing a calculation. This purpose manifests syntactically where the onClick() method is void and therefore behaves more like a listener.

For that reason, I would call the code example a degenerate form of the Observer Pattern. It looks to me like an Observer attempt that didn't quite meet the qualifications. I wouldn't dignify the attempt by associating it with another pattern.

Post a Comment for "Is Using Onclicklistener() An Example Of Strategy Pattern?"