Results 1 to 4 of 4
  1. #1
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default MVC separation of data

    I am currently tasked with developing a vending machine simulation where I will have to have three interfaces, one each for Manager, Restocker and Customer. I am trying to implement my design so that the view will not see the actual objects from the Inventory. This works fine for the customer because all I need to do is provide the name of the product in a location. The problem I am having is with the Restocker and Manager, when they view data in the inventory they are going to want details like expiration date and product type but in order to easily provide that data I would have to pass the actual Product object and that doesn't seem right, to allow a view to have access to the actual Product object.

    I guess my question is, am I correct in trying to keep the actual Product object from being passed to a view? Should I do something like clone the product and send it that way. If I clone the product, then a view can make all the changes they want (not that they should) and it won't affect the original product. My goal is to force the view to use the controller to manipulate the actual inventory.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,039
    Rep Power
    20

    Default Re: MVC separation of data

    Why does the view need to make any changes to the Product?
    If you simply want to ensure it doesn't then either make Product immutable or wrap in something immutable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default Re: MVC separation of data

    The view doesn't need to make changes to the product, in fact, I don't want it to and that's my problem. I realize now that I implied the opposite by stating "the view can make all the changes it wants" but what I meant was, if some malicious view wanted to change the data, they could but it wouldn't affect the vending machine's actual internal data.

    The product has a UPC, expiration date, name, description, type and image... If I want a view that will display the UPC, Name and Image it would be easy to just have a getProductAtLocation(someLocation) return a type Product but if some developer decides to use my VendingMachine package and implement a class that calls getProductAtLocation(...) I don't want them to be allowed to alter the actual Product object kept in the internal inventory of the main VendingMachine class, only view it. Kind of a "By Value" as opposed to "By Reference".

    I think making the Product immutable could work. I hadn't thought of that. But I also still am wondering, is this "best practice"? Should my model or controller be returning the Product object so that the view can access all of the details of that particular object?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,039
    Rep Power
    20

    Default Re: MVC separation of data

    If there is no need for anyone to change the value then making it immutable is the ideal.
    After all, it's not supposed to change once created.
    If something (eg the VendingMachine) needs to be able to change it after creation then wrapping it in an immutable wrapper is the best bet.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 11
    Last Post: 09-21-2012, 09:33 AM
  2. Getting problem inserting data in data base
    By anupama in forum New To Java
    Replies: 4
    Last Post: 12-15-2010, 10:03 PM
  3. Replies: 0
    Last Post: 10-31-2008, 02:13 PM
  4. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 AM
  5. Replies: 7
    Last Post: 05-23-2008, 03:46 AM

Tags for this Thread

Posting Permissions

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