In MVC where do you put references to your model Classes?

前端 未结 5 1672
野的像风
野的像风 2021-02-04 06:33

I have been wondering for a while, after asking different people and without any of them providing what I would call an \"at least a bit concrete answer\":

Quest

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 07:30

    Where, in an iPhone application should an application keep the references to it's Model Classes ( using the MVC approach) ?

    The controller layer keeps references to the model layer.

    So is the App Delegate a controller ? a model class ? none of the two ?

    The App Delegate is a controller.

    Where would you guys store the references to A and B ?

    A and B are model classes that would usually be created and owned by the controller layer.

    I would really like to know how would you guys use mvc to put together this application which only uses one View.

    The purpose of the controller layer is to allow the model and view layers to be self-contained. The model shouldn't know anything about the controller or view layers. The view shouldn't know anything about the controller or model layers. The controller's job is to be a double-ended adapter to the model on one side and the view on the other.

    I would set up your example app like this:

    • UIApplication delegates to AppDelegate.
    • If operation of your server class (A) is simple:
      • AppDelegate creates and owns instance(s) of server class A.
    • If operation of your server class (A) is complicated:
      • Create a dedicated controller class (C) to control the server.
      • AppDelegate creates and owns instance(s) of class C. One instance of (C) for each instance of (A).
      • Each instance of class C creates and owns one instance of class A.
    • AppDelegate creates and owns an instance of your ViewController class, which loads and owns your view.

    It's not clear in the question what the purpose of class B is.

    • If it's a chunk of data that's for the use of A only (like config data or static website data), I would have it be created and owned by the server (A).
    • If it's data that is created during server operation and needs to be displayed in the view, then you will probably want something like:
      • A mutable array owned by A, to hold instances of B.
      • Another controller class (D) to reference that array and act as a datasource/delegate to your view.

提交回复
热议问题