Skip to content Skip to sidebar Skip to footer

Correct Way Of Applying Delegation Pattern In Android

I am trying to use the delegation design pattern in my android application but not sure whether I am doing it correctly or not. Here is my code for LoginActivity.java protected vo

Solution 1:

Your code is delegating but IMO not using "the" delegation pattern.

new ConnectDatabase().authenticate... could simply be written as ConnectDatabase.authenticate and would then be a static utility mehod. You don't need an object at all.

From Wikipedia

an object, instead of performing one of its stated tasks, delegates that task to an associated helper object. There is an Inversion of Responsibility in which a helper object, known as a delegate, is given the responsibility to execute a task for the delegator.

I would argue that you need to take it a little more literally when above states that you delegate to an associated object. For one because objects need to have a reason to be objects, they need a responsibility, they need to encapsulate something. It could for example keep track of some state for you and hide some complex state machine logic from your Activity. There is also no association between those two objects when the helper is created and thrown away in the same line.


Post a Comment for "Correct Way Of Applying Delegation Pattern In Android"