Does it violate the MVC pattern's separation of concerns if a model class contains a static method whose parameters are instances of a View object?

StackOverflow https://stackoverflow.com/questions/23402577

Question

public static Intent prepare( EditText to, EditText cc, EditText subject, EditText content ){

Intent preparedIntent = new Intent( Intent.ACTION_SEND );
preparedIntent.setType( "plain/text" );
preparedIntent.putExtra(Intent.EXTRA_EMAIL, new String[ ] 
{ 

    to.getText().toString( )

});
preparedIntent.putExtra(Intent.EXTRA_CC, new String[ ]
{

cc.getText( ).toString( )

});
preparedIntent.putExtra(Intent.EXTRA_SUBJECT,subject.getText().toString());

preparedIntent.putExtra(Intent.EXTRA_TEXT,content.getText().toString());

return preparedIntent;}

Does it violate the MVC pattern's separation of concerns if a model class contains a static method whose parameters are instances of a View object?

I'm trying to research whether it is not a proper implementation of the MVC pattern's separation of concerns if I choose to provide a static helper method in my Email.java class that is used for returning an instance of an intent within the scope of the email client's activity class. The reasons for me questioning this design decision is that even though an intent is not an instance of View however I am passing objects that are instances of View objects within the scope of the Email.java (model) class.

Therefore, assuming I am not conforming to the MVC pattern, would changing the parameter types to String and having each instance of View return an object of type string by calling its viewObject.getText().toString() within the parameter construct everytime I wish to invoke the Email.prepare( .....) method then allow for conformity to the MVC pattern by utilizing proper separation of concerns?

Here is a link to the code if it better helps in possibly forming an answer. The two classes in question are the EmailActivity.java file in the activities package and the Email.java file in the models package.

Thank you for any input by the way ahead of time. I was just curious about this. It is obviously not a major issue and more minor but I am trying to learn design patters. I know the general answer is "well you're the designer!".

Was it helpful?

Solution

Your code violates a couple of accepted coding practices but not necessarily MVC. Here is how I would re-factor it so that it is DRY and follows LoD.

public static Intent prepare( String to, String cc, String subject, String content, String messageType )
{

    if (messageType==null){ messageType = "plain/text"; }

    Intent preparedIntent = new Intent( Intent.ACTION_SEND );
    preparedIntent.setType( messageType );

    if (to!=null) {
        preparedIntent.putExtra(Intent.EXTRA_EMAIL, new String[ ] 
        { 
            to
        });
    }

    if (cc!=null) {
        preparedIntent.putExtra(Intent.EXTRA_CC, new String[ ]
        {
            cc
        });
    }
    preparedIntent.putExtra(Intent.EXTRA_SUBJECT, subject);

    preparedIntent.putExtra(Intent.EXTRA_TEXT, content);

    return preparedIntent;
}

This way, you don't have to reference a specific type of control (EditText) and you can re-use this static method anywhere you need to create an email Intent

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top