3

I am learning MVC patterns. I have created this very simple code as a demo.

The target of the code is simple. I have created a car object which has a initial speed of 95 miles per hour. there are 1 label to show the current speed, 2 buttons, increase and decrease speed.

on clicking each of the buttons, the speed is incremented or decremented by 1 and the label shows the modified speed. once the speed exceeds 100, the increase button is disabled, user can only decrease the speed now. once the speed is decreased below 100, the increase button is enabled again.

this is the codes for my Model, View and Controller.

Interface IModel

public interface IModel
{
    int getCurrentSpeed();

    int increaseSpeed();
    int decreaseSpeed();

    void updateObserver();
    void setView(IView objView);
}

Class carModel (concrete class from IModel)

public class carModel : IModel
{
    private IView objLocalView;
    private int currentSpeed;

    //Constructor
    public carModel(int carSpeed)
    {
        this.currentSpeed = carSpeed;
    }

    public void setView(IView objView)
    {
        objLocalView = objView;
    }

    public int increaseSpeed()
    {
        currentSpeed++;
        updateObserver();
        return currentSpeed;
    }

    public int decreaseSpeed()
    {
        currentSpeed--;
        updateObserver();
        return currentSpeed;
    }

    public void updateObserver()
    {
        objLocalView.updateStatus(this);
    }

    public int getCurrentSpeed()
    {
        return currentSpeed;
    }

}

Interface IController

public interface IController
{
    void requestIncreaseSpeed();
    void requestDecreaseSpeed();
}

Class carController (concrete class from IController)

public class carController : IController
{
    private IModel objLocalModel;
    private IView objLocalView;

    //constructor
    public carController(IModel objModel, IView objView)
    {
        objLocalView = objView;
        objLocalModel = objModel;
        objLocalModel.setView(objView);
    }

    public void requestIncreaseSpeed()
    {
        int modifiedSpeed = objLocalModel.increaseSpeed();
        checkSpeedLimit(modifiedSpeed);
    }

    public void requestDecreaseSpeed()
    {
        int modifiedSpeed = objLocalModel.decreaseSpeed();
        checkSpeedLimit(modifiedSpeed);
    }

    public void checkSpeedLimit(int modifiedSpeed)
    {
        if (modifiedSpeed > 100)
        {
            objLocalView.disableIncreaseSpeed();
        }
        else
        {
            objLocalView.enableIncreaseSpeed();
        }
    }

}

interface IView

public interface IView
{
    void enableIncreaseSpeed();
    void disableIncreaseSpeed();

    void updateStatus(IModel objModel);
}

form1.cs (the view class which users will see)

public partial class Form1 : Form, IView
{
    private IModel objModel;
    private IController objController;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        objModel = new carModel(95);
        objController = new carController(objModel, this);

        updateStatus(objModel);
    }

    public void enableIncreaseSpeed()
    {
        btnIncrease.Enabled = true;
    }

    public void disableIncreaseSpeed()
    {
        btnIncrease.Enabled = false;
    }

    public void updateStatus(IModel objModel)
    {
        lblCurrentSpeed.Text = "";
        lblCurrentSpeed.Text = objModel.getCurrentSpeed().ToString();
    }

    private void btnIncrease_Click(object sender, EventArgs e)
    {
        objController.requestIncreaseSpeed();
    }

    private void btnDecrease_Click(object sender, EventArgs e)
    {
        objController.requestDecreaseSpeed();
    }
}

I am not sure if this is a proper MVC design? because I cant see much differences from a regular 3 tier architecture (apart from the Model and View are communicating here, which does not happen in 3 tier architecture)

Manas Saha
  • 1,477
  • 9
  • 29
  • 44
  • Should the *model* update the *view*? I'm not sure, I believe that the **controller** should do it. – Andre Calil Jul 31 '12 at 05:30
  • Hi Andre, I am not sure, if all the communication between model and view happens via the controller then there will be no difference between MVC and a classic 3 tier architecture? – Manas Saha Jul 31 '12 at 05:34
  • In fact, I'm not a MVC expert. I'd rather talk about *web* MVC, and that's why I believe that the controller should be in charge of any communication with the view. Model should only care about its properties (like validation an so on). But, as I said, I'm not sure. – Andre Calil Jul 31 '12 at 05:36
  • Your code looks fine to me. Good that you have used dependency injection ever where. For your original question, its possible duplicate of this SO thread http://stackoverflow.com/questions/698220/mvc-vs-n-tier-architecture – Anand Jul 31 '12 at 06:32

1 Answers1

0

Since you are trying to implement classical MVC, the information flow is about right.

Only thing, that seems a bit off, is "Model". You have assumed that model is a class. Well ... it isn't. Model should be a layer, which encompasses all of domain business logic.

tereško
  • 58,060
  • 25
  • 98
  • 150