Rice University - Comp 212 - Intermediate Programming

Fall 2001

Lecture #16 - Model-View-Controller ("MVC") and Inner Classes


 

MVC summary (See the MVC design pattern)

The MVC architecture decouples the model from the view, allowing each to be changed without affecting the other.

Model vs. View


Inner Classes (see the Java Resources web page)

  1. Named inner classes

    1. Used when more than one instance is needed.
    2. Scoped to private of the instantiating outer object.
    3. Can have parameterized constructors.

     

  2. Anonymous Inner classes

    1. Used when only one instance is needed.
    2. Scoped to local of the instantiating method of the outer object.
    3. Accessing local variables:
      1. local variables must be declared final, including input parameters
      2. Final variables cannot have their values changed. However, this does not mean that if the variable is an object reference, that the internal fields of the object can't be changed.
    4. Can't have a parameterized constructor --> since the anonymous inner class can access everything in the outer class anyway, this is often not a problem, but sometimes causes initialization problems.
    5. Initializer blocks. -- run before the constructor. Can be used to transfer data.

     

  3. Referencing the inner and outer classes from inside the inner class

    1. "this" refers to the inner class instance.
    2. "[Outer class name].this" references the outer class instance.
    3. There is a problem for multiple nested inner classes -- use initializer blocks to save the "this" references to named variables.

     

  4. Anonymous inner classes as lambda's

    1. Just as a function with no name, we have an class with no name.
    2. The outer instance is the inner instance's closure (environment).
    3. The anonymous inner class's behavior depends on the environment in which is was created.
      1. Factories with static parameters.
      2. Factories with dynamic paramters: Curried Functions
    4. Solving problem of needed multiple interfaces with different symantics by using multiple factory methods.
    5. Processing recursive data structures filled with abstract data -- word lists and body parts redone.