1

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 void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }
    public void login(View v) {
        AutoCompleteTextView emailInput = (AutoCompleteTextView) findViewById(R.id.email);
        EditText passwordInput = (EditText) findViewById(R.id.password);
        String email = emailInput.getText().toString();
        String password = passwordInput.getText().toString();
        new ConnectDatabase().authenticate(email, password);
    }

and here is my ConnectDatabase.java delegator

public class ConnectDatabase {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }
    public boolean authenticate(String email, String password) {
        Log.d("Message", email+" &"+password);
        return false;
    }

}

Is it the correct way of using the design pattern or I have to make a interface and then let the LoginActivity.java implement it?

jayesh.doolani
  • 39
  • 1
  • 10
  • What do you mean by "correct way"? Does your code work as expected? – Anton Savin Nov 10 '14 at 21:26
  • It works but am I implementing the design pattern correctly or it may have some flaws? – jayesh.doolani Nov 10 '14 at 21:27
  • This question appears to be off-topic because it is about code review. – Anton Savin Nov 10 '14 at 21:28
  • I read it somewhere that we have to use an interface and then make the other classes to implement that interface. But here I am just accessing that class using an object. Which is a better way, through interface or via simple call? – jayesh.doolani Nov 10 '14 at 21:31
  • The question is quite broad because delegation can be a simple method call or as complex as a multi-interface paradigm. So, yes you are using delegation, but its the context which makes it viable. – David Pullar Nov 10 '14 at 21:32

1 Answers1

0

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.

zapl
  • 63,179
  • 10
  • 123
  • 154