SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 71
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Brazil,Maringá-PR
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Calculator Design (MVC)

    Most of us, are seen MVC for the first time, direct in a web enviromment.
    So, to help to abstract this concept, I think would be interesting, develop an app in PHP as it was in desktop.

    I sugest develop a calculator application, with all the Model View and Controller parts.

    The idea is forget the tradicional web flow, and think as an application.

    Let's start with the Model.

    Since Model is completely separated and independent from the Controller and View, we should start developing from it, in a so isolated way, that I could use it even in a PROMPT enviromment.

    The pre-requisites are Implement the follow calculator operations. Let's take the windows calculator as inspiration:
    Binary operations: + - * /
    Unary operations: sqrt factorial x^2

    Use only PHP5 with ESTRICT OFF to be able to use simpletest.
    Full OO and Design Patterns

    Can we start? any suggestion?

  2. #2
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by neves
    The idea is forget the tradicional web flow, and think as an application.
    This is not really possible, because Web is inherently stateless, while "traditional" applications can change state dynamically.

    The only way I see this can be done solely in PHP is using simple Web form wih buttons, with each click refreshing the whole form and the display of the number.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or with PHP-GTK we could have a real desktop application. However, you then cannot, however, you PHP 5, and I have a feeling this is not what neves wants to do.

  4. #4
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could write a socket app for PHP that takes commands and returns data. I've personally working on this right now and it works nicely. Basically the same as a shell/c++ command line program.

  5. #5
    SitePoint Addict timvw's Avatar
    Join Date
    Jan 2005
    Location
    Belgium
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in a shell i prefer "bc" to do math stuff

    a little websearch will show there are plenty of javascript calculators too..

    and writing a php version should'nt be too hard either.
    i was bored once and write a very dirty sample..
    http://timvw.madoka.be/programming/php/calculator.txt

  6. #6
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by petesmc
    You could write a socket app for PHP that takes commands and returns data. I've personally working on this right now and it works nicely. Basically the same as a shell/c++ command line program.
    That is true, the stream functions in PHP5 are pretty good, however I think it adds an unneccessary complexity to the application. How would the client be written? You could use AJAX, but is it going to far for a calculator?

    I didn't realise neves meant in the command line (I assume that is what you mean by PROMPT enviroment). I thought he meant a graphical application. Now that I do see this, the application should be easily achievable. However, often a calculator in the command line should be a small thing. Maybe something like Python in interactive mode. Does this really require MVC?

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Brazil,Maringá-PR
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Please, don't loose the point.
    We don't want here, build a calculator, but study MVC.
    I just take a calculator as example cause it is simple and every body knows how it works.

    This is not really possible, because Web is inherently stateless, while "traditional" applications can change state dynamically.

    The only way I see this can be done solely in PHP is using simple Web form wih buttons, with each click refreshing the whole form and the display of the number.
    That's the point. The MVC concept, should be the same even in a web or desktop app. So studing an application that works on web and command line, we can clearly see what is MVC and what are specific for web and for desktop.

    Or with PHP-GTK we could have a real desktop application. However, you then cannot, however, you PHP 5, and I have a feeling this is not what neves wants to do.
    That's right. The desktop version, will be only command line.

    i was bored once and write a very dirty sample
    We are bored too of those dirty sample. So, how can we build the same code, using OO, Patterns and MVC? That the point of this thread.

    That is true, the stream functions in PHP5 are pretty good, however I think it adds an unneccessary complexity to the application. How would the client be written? You could use AJAX, but is it going to far for a calculator?
    The question is. If I have a MVC calculator working, what I need to change to add an AJAX client?
    I didn't realise neves meant in the command line (I assume that is what you mean by PROMPT enviroment). I thought he meant a graphical application.
    PROMPT == command line
    A graphical application is not needed right now.
    Now that I do see this, the application should be easily achievable. However, often a calculator in the command line should be a small thing.
    Maybe something like Python in interactive mode. Does this really require MVC?
    A calculator should be a small thing, but has greate requirements to study MVC.

    I don't we try?

  8. #8
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic: Anyone got some easy reads on MVC?

  9. #9
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by neves
    The MVC concept, should be the same even in a web or desktop app. So studing an application that works on web and command line, we can clearly see what is MVC and what are specific for web and for desktop.
    The very nature of the web makes this impossible, as pointed out.

    To get something back, on the web, you have to make a request and wait for a response. There's no getting around that. AJAX does it without a page reload, but it still involves making a request to the server and waiting for a response.



    As for a simple look at my take on how this would work in MVC, let's assume the user has requested the result of "9 + 9". Very simple.

    PHP Code:

    $numbers 
    = array( '9' '9' );

    $controller = new AdditionController$numbers );

    class 
    AdditionController {
      private 
    $numbers;
      private 
    $model;
      private 
    $view;
      function 
    __construct$numbers ){
        
    $this->numbers $numbers;
        
    $this->createModel();
        
    $this->createView();
      }
      private function 
    createModel(){
        
    $this->model = new AdditionModel$numbers );
      }
      private function 
    createView(){
        
    $this->view = new AdditionView();
      }
      public function 
    execute(){
        
    $this->model->execute();
        
    $result $this->model->fetch();
        
    $this->view->display$result );
      }
    }

    class 
    AdditionModel {
      private 
    $number1;
      private 
    $number2;
      private 
    $result;
      public function 
    __construct$numbers ){
        
    $this->number1 $numbers[0];
        
    $this->number2 $numbers[1];
      }
      public function 
    execute(){
        
    $this->result $this->number1 $this->number2;
      }
      public function 
    fetch(){
        return 
    $this->result;
      }
    }

    class 
    AdditionView {
      private 
    $content;
      public function 
    __construct(){}
      public function 
    display$answer ){
        
    $this->content $answer;
        echo 
    $this->content;
      }

    Pretty sloppy, but it illustrates the idea well enough. Obviously this is a very simplistic version, but you can easily make it dynamic enough to accept whatever operation and variables are given.

    The View would, in my applications, consist of loading an XSL stylehsheet and the Model return pure XML which would transformed to XHTML (or whatever output is needed).

  10. #10
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by neves
    The MVC concept, should be the same even in a web or desktop app.
    Perhaps I don't really understand what you're trying to say, but this sentence is simply incorrect.

    The classic, "desktop" concept of MVC depends on the states of its member objects: when a user does something to Controller (say, presses a calculator button), it tells the Model to change its state and the View to check the Model's state and alter its own state accordingly (ie. displays a new value). All those objects are in-memory, and their state changes dynamically.

    On the Web, there is no state. When a user presses a button or clicks a link, a request is sent to the server, which returns a complete response. The View has no dynamic knowledge of the Model's state, and between two requests the Model's state might change completely. PHP closely follows this stateless concept, and although some state-like features are available through sessions and other forms of persistence it still relies on this basic response/request model.

    This is the most important point when dealing with MVC -- this pattern was originally invented for stateful environments like desktop, while in stateless ones like Web it has to be evolved into variants like Front Controller and Page Controller. That is the whole point of patterns: they are solutions which should be used when they fit the problem instead of trying to fit the problem into a solution.

  11. #11
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After seeing that example of MVC for the calculator, I can say it's completely not what a calculator should be written in. Why? Because calculators aren't based on actions like addition and subtraction all seperately. They are based on parsing a string of characters and organising them appropriately. I beleive it's called BNF or something like that and involves dynamic stacks (not php). Basically, the only action/command is to parse and execute. It seems pointless adding so much bloated code just to do an addition command.

  12. #12
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would 100% agree.

  13. #13
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Brazil,Maringá-PR
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Viflux
    The very nature of the web makes this impossible, as pointed out.

    To get something back, on the web, you have to make a request and wait for a response. There's no getting around that. AJAX does it without a page reload, but it still involves making a request to the server and waiting for a response.
    A remote request or memory request is, at a hight level, the same thing.

    Quote Originally Posted by Viflux
    As for a simple look at my take on how this would work in MVC, let's assume the user has requested the result of "9 + 9". Very simple.
    "9 + 9" is just a kind of input. What about, 1 request sending 9, other sending the operator, and other sending the second 9, at end, the = operator that brings the result.
    I'll put an online form to make it clearly.

  14. #14
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Brazil,Maringá-PR
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    Perhaps I don't really understand what you're trying to say, but this sentence is simply incorrect.

    The classic, "desktop" concept of MVC depends on the states of its member objects: when a user does something to Controller (say, presses a calculator button), it tells the Model to change its state and the View to check the Model's state and alter its own state accordingly (ie. displays a new value). All those objects are in-memory, and their state changes dynamically.

    On the Web, there is no state. When a user presses a button or clicks a link, a request is sent to the server, which returns a complete response. The View has no dynamic knowledge of the Model's state, and between two requests the Model's state might change completely. PHP closely follows this stateless concept, and although some state-like features are available through sessions and other forms of persistence it still relies on this basic response/request model.
    There's no different. What you tell me about java, that the application stay in memory between requests? The same can be done with php using some kind of persistence.

  15. #15
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by neves
    There's no different. What you tell me about java, that the application stay in memory between requests? The same can be done with php using some kind of persistence.
    It has nothing to do with Java vs. PHP, it's about stateless vs. stateful.

    The point is that you have two environments in a Web application: server and browser. Each has its own memory space and working logic -- what connects them is the HTTP response/request mechanism.

    A Java Web app does indeed stay in memory between requests (unlike PHP), but it's completely besides the point -- at any moment in time, the browser sees only what the server sends at that moment, and has no knowledge of any subsequent changes on the server until the next request.

  16. #16
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It can't be done with Java over the web either.

    Unless you're talking about an applet, in which case the applet is loaded and resides in the client machines memory.

  17. #17
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Brazil,Maringá-PR
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by petesmc
    After seeing that example of MVC for the calculator, I can say it's completely not what a calculator should be written in. Why? Because calculators aren't based on actions like addition and subtraction all seperately.
    This is not how windows Calculator, and almos all hand ones, works. Each digit pressed, is a command.

    Quote Originally Posted by petesmc
    They are based on parsing a string of characters and organising them appropriately. I beleive it's called BNF or something like that and involves dynamic stacks (not php). Basically, the only action/command is to parse and execute.
    This is one way, but not the only and with shure, not the easier.

    Quote Originally Posted by petesmc
    It seems pointless adding so much bloated code just to do an addition command.
    I don't know why programmers have problems with this:
    "Like all of my examples it's one of those super-simple examples; small enough to be unreal, but hopefully enough for you to visualize what's going on without falling into the bog of a real example." Martin Fowler http://www.martinfowler.com/articles/injection.html

  18. #18
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Brazil,Maringá-PR
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Viflux
    It can't be done with Java over the web either.

    Unless you're talking about an applet, in which case the applet is loaded and resides in the client machines memory.
    As BerislavLopac clarify, a servlet stay in memory at the server side, keeping the business logic and state alive.
    The same can be done with php, just persisting it's state.

  19. #19
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by neves
    This is not how windows Calculator, and almos all hand ones, works. Each digit pressed, is a command.
    And thats why Windows Calculator sucks big time.

    Edit: Oh and almost all decent hand calculators use the method i mentioned.

    This is one way, but not the only and with shure, not the easier.
    It is actually insanely easy to implement provided you have stacks available.

    I don't know why programmers have problems with this:
    "Like all of my examples it's one of those super-simple examples; small enough to be unreal, but hopefully enough for you to visualize what's going on without falling into the bog of a real example." Martin Fowler http://www.martinfowler.com/articles/injection.html
    Why use bloated code when it gives no extra functionality, extendability, reusability or ease of reading?! I just think MVC is not suited to a calculator, that's it. It IS suited to many other things though.

  20. #20
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes the servlet stays in memory, but your browser doesn't know about it until a request is made.

    The servlet doesn't send updates to the browser without receiving a request.

    In traditional MVC, the view draws updates (changes the GUI) based on the model. No round-trip request is necessary.

  21. #21
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Brazil,Maringá-PR
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    At any moment in time, the browser sees only what the server sends at that moment, and has no knowledge of any subsequent changes on the server until the next request.
    The same happens in a desktop enviromment, but in a different scale.

  22. #22
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wrong.

    Think about when you're installing a program. The status bar is changing without interaction from you. It's being updated by the "View" layer of the code.

    You didn't press a button each time the bar fills a little more, the program updates the GUI without interaction.

  23. #23
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Brazil,Maringá-PR
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Viflux
    Yes the servlet stays in memory, but your browser doesn't know about it until a request is made.

    The servlet doesn't send updates to the browser without receiving a request.

    In traditional MVC, the view draws updates (changes the GUI) based on the model. No round-trip request is necessary.
    The fact that server doesn't send updates to the browser, doesn't destroy the MVC concept, that's is nothing more than: "Be able to separate Model and View". It's simple that!

  24. #24
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Brazil,Maringá-PR
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Viflux
    Wrong.

    Think about when you're installing a program. The status bar is changing without interaction from you. It's being updated by the "View" layer of the code.

    You didn't press a button each time the bar fills a little more, the program updates the GUI without interaction.
    It's just a behavior of web, that can be fixed using JS, iframe, refresh, flash, etc...
    It does not breaks MVC concept.

    http://pear.laurent-laville.org/HTML...ntal/model.php

  25. #25
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh dear, neves, you are persistent. I don't know how to word my explanations anymore, since you have a way to twist them out of the context and use them to (unsuccessfuly) prove your point.

    Quote Originally Posted by neves
    It's just a behavior of web
    And that is the whole point. In your reasoning: I can't see why cars wouldn't be able to fly -- it's just a behavior of airplanes and it can be fixed with rockets, wings and antigravity devices.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •