Rice University - Comp 212 - Intermediate Programming
Fall 2001
Lecture #16 - Model-View-Controller ("MVC") and Inner
Classes
-
The Model
- The "guts" of the program that actually contains the logic
of how the program runs.
- Requires the knowledge of the interfaces used for it to communicate
with the rest of the system.
-
The View
- The rest of the system w.r.t. the model. -- notice this isn't exactly
what I said on in the last lecture.
- Requires the knowledge of the interfaces used for it to communicate
with the rest of the system.
-
The Controller.
- Instantiates the model
- Instantiates the view
- Instantiates and installs the adapters that are used to communicate
between the model and the view.
- Minimal logic in the adapters -- their job is simply to translate one
syntax to another.
- All that needs to be done to create the model+view system is to instantiate
the controller and tell it to construct the system. -- e.g. the main()
method of an application will instantiate the controller and tell it to
construct the system, period.
The MVC architecture decouples the model from the view,
allowing each to be changed without affecting the other.
Model vs. View
- Is there really a difference between the model and the view? --> The
model and the view are on equal footing. Model vs. view is a matter of viewpoint
only.
- The system topology is not restricted to a single model and a single
view.
- Nor is a model/view confined to communicating with a single view/model.
- Multi-layer topologies
- Branched topologies
- Does a model/view "know" how many different views/models it is
communicating with?
- The number of adapters and the methods that appear on them should be
determined by the distinct sets of operations or services that the model/view
needs from its outside world.
- Group related methods into a single adapter.
- Separate unrelated methods into different adapters.
- Note that the adapter itself can still redefine the groupings by communicating
to different targets for different methods.
-
Named inner classes
- Used when more than one instance is needed.
- Scoped to private of the instantiating outer object.
- Can have parameterized constructors.
-
Anonymous Inner classes
- Used when only one instance is needed.
- Scoped to local of the instantiating method of the outer object.
- Accessing local variables:
- local variables must be declared final, including input parameters
- 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.
- 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.
- Initializer blocks. -- run before the constructor. Can be used to transfer
data.
-
Referencing the inner and outer classes from inside the inner class
- "this" refers
to the inner class instance.
- "[Outer class name].this"
references the outer class instance.
- There is a problem for multiple nested inner classes -- use initializer
blocks to save the "this" references to named variables.
-
Anonymous inner classes as lambda's
- Just as a function with no name, we have an class with no name.
- The outer instance is the inner instance's closure (environment).
- The anonymous inner class's behavior depends on the environment in which
is was created.
- Factories with static parameters.
- Factories with dynamic paramters: Curried Functions
- Solving problem of needed multiple interfaces with different symantics
by using multiple factory methods.
- Processing recursive data structures filled with abstract data -- word
lists and body parts redone.