Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this code a proper MVC architecture?
    primarykey
    data
    text
    <p>I am learning MVC patterns. I have created this very simple code as a demo. </p> <p>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. </p> <p>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.</p> <p>this is the codes for my Model, View and Controller.</p> <h2>Interface IModel</h2> <pre><code>public interface IModel { int getCurrentSpeed(); int increaseSpeed(); int decreaseSpeed(); void updateObserver(); void setView(IView objView); } </code></pre> <h2>Class carModel (concrete class from IModel)</h2> <pre><code>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; } } </code></pre> <h2>Interface IController</h2> <pre><code>public interface IController { void requestIncreaseSpeed(); void requestDecreaseSpeed(); } </code></pre> <h2>Class carController (concrete class from IController)</h2> <pre><code>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 &gt; 100) { objLocalView.disableIncreaseSpeed(); } else { objLocalView.enableIncreaseSpeed(); } } } </code></pre> <h2>interface IView</h2> <pre><code>public interface IView { void enableIncreaseSpeed(); void disableIncreaseSpeed(); void updateStatus(IModel objModel); } </code></pre> <h2>form1.cs (the view class which users will see)</h2> <pre><code>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(); } } </code></pre> <p>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)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload