HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

First introduction to MVC

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
introductionfirstmvc

Problem

I'm not sure that I fully understand this pattern. I've written a simple application that uses MVC. Please criticize my approach to the use of this pattern. For convenience, I'll post the source code of the main classes. The source code of the project can be found here: SoundLights on GitHub

The app has one screen. Screen background might have 4 different images. To change current screen background the user is using Next and Previous buttons. Buttons Play and Pause(Stop) are used to control for the soundlights. The soundlights is matrix of colored squares (translucent squares, kind of filters).

I've used 2 different models:

  • BackgroundModel - to process changing background



  • SoundLightsModel - for working with the soundlights.



SoundLightsActivity.java

public class SoundLightsActivity extends Activity {

    private SoundLightsController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        controller = new SoundLightsControllerImpl(this);
    }

    @Override
    protected void onDestroy() {
        controller.onDestroy();
        super.onDestroy();
    }
}


SoundLightsController.java

public interface SoundLightsController {

    void onStart();

    void onStop();

    void onDestroy();

    void onPrevious();

    void onNext();
}


SoundLightsControllerImpl.java

```
public class SoundLightsControllerImpl implements SoundLightsController {

private final SoundLightsModel soundLightsModel;
private final SoundLightsView view;
private final BackgroundModel backgroundModel;

public SoundLightsControllerImpl(Activity activity) {
this.view = new SoundLightsViewImpl(activity, this);
this.soundLightsModel = new SoundLightModelImpl(view);
this.backgroundModel = new BackgroundModelImpl(view, (Context) activity);
}

@Override
public void onStart() {
view.setInvisibleStartButton();
view.setVisibleStopButt

Solution

I've only skimmed through, and all I could find is a bool stopped which I would have called bool isStopped, although stopped is a fairly unambiguous, clear name - as is the rest of this code.

Oh, and maybe the 256 hard-coded into your ColorRandomizer could be a constant.

Naming is crystal-clear and consistent, indentation is perfect; simple, neat, ...nothing to add.

As for MVC, well I can't talk about Java specifics (never wrote a line of Java), but design patterns are a mean of communication, so when you say "I do X in my controller", fellow programmers know what you're talking about.

  • The Model contains data that the View needs to render.



  • The View contains your markup.



  • The Controller receives a request and responds with a view.



Given that, I believe your model should know nothing about a view - rather, it's the opposite: the view needs to know about the model. Now I'd love to see another answer from an actual Java developer, because I've only done MVC with ASP.NET in C#, so I'm hoping for this to be confirmed or corrected :)

Context

StackExchange Code Review Q#28419, answer score: 5

Revisions (0)

No revisions yet.