Tuesday, October 19, 2010

(IN PROGRESS) An instructional walk-thru.

Learning the MVP pattern is easier by example.

As such, the following table is only to plant a tiny seed in your mind.  Clarity will come by working through examples and then experimenting on your own.

Part Purpose
IModel Data
IView UI
Presenter Intermediary between View and Model.

You have been asked to write a program that lets its user pick two currency types and convert an amount from one to the other.

You define an enumeration type to represent the supported kinds of currency.

public enum CurrencyKind
   USD, // U.S. Dollar
   EUR, // Euro
   AUD // Australian Dollar
   // Additional currencies may be added here.

You define an interface that specifies the functionality required to convert one currency to another.

public interface IModel 
   double ConvertCurrency(
    CurrencyKind from,
    CurrencyKind to,
    double amount);

You define an interface that specifies the movement of information to and from the UI.  Note the use of properties and events to model the direction of data flow. If you're wondering what exactly it is that the data will flow between, dont worry, that's the right question. Stay tuned. (Hint: if you guessed "View and Presenter" you were right)
public interface IView
   CurrencyKind FromCurrencyKind { get; }
   CurrencyKind ToCurrencyKind { get; }
   double ConvertedAmount { set; }
   event EventHandler ConvertCurrency;

You do not want to worry about the details of currency conversion right now so you make a temporary implementation of the model that will suffice.

public class TemporaryModel : IModel 
   double ConvertCurrency(
     CurrencyKind from, CurrencyKind to, double amount)
       // Hard-coded conversion.
       // For now the result is always twice the amount.
       return amount * 2;

TODO: stub the presenter

TODO: Implement first version of concrete View.

Main Program
TODO: Create a driver program to create the objects and get things going.

Test from windows live....



No comments:

Post a Comment