Graphics Reference
In-Depth Information
Inheritance Versus Composition
Whenclass-basedOOPappeared,theinheritance mechanismwasusedtoreusecode
and to realize dynamic binding and polymorphism. A subclass can use all code in its
superclass just by inheritance. In other words, the code in the superclass is reused by
the subclass easily.
Dynamic binding means that the methodimplementation that is actually invoked
is decided at runtime. Polymorphism means that one method can handle objects be-
longing to different types in order to respond differently according to the right type-
specific behavior. Polymorphism is implemented at runtime using dynamic binding.
headvantage ofdynamicbindingandpolymorphismisthat theymaketheinterface
ofamethodsimpleandmakethecodeeasiertochange.
We can also realize code reuse, polymorphism and dynamic binding by using
composition, which means the use of instance variables that are references to other
objects.
Generally speaking, composition is better than inheritance for realizing reusabil-
ity and polymorphism. In an inheritance relationship, superclasses are oten fragile,
becauseachangetoaninterfaceofsuperclasscanrequirechangesinmanyothercode
segments that use the superclass or any of its subclasses. hus, inheritance should be
used only when a subclass “is-a” superclass. If we want to reuse code or to realize
polymorphism, and there is no natural is-a relationship between objects, we should
use composition.
MVC Design Pattern
17.2.5
he Model-View-Controller (MVC) design pattern is not included in GoF design
patterns. It is, however, an important design pattern (or framework), especially for
graphics programming. It was originally developed for the GUI of the Smalltalk sys-
tem (Krasner and Pope, ) and has been widely used in many systems, including
Java. he MVC pattern breaks an application into three parts: a model, a view and
a controller.
A model is used to manage information and notify observers when information
changes. It contains only data and functionality that are related by a common pur-
pose. A model is meant to serve as a computational approximation or abstraction
of some real world process or system. It captures not only the state of a process or
system, but how the system works.
A view is responsible for mapping graphics of a model onto a device. A view has
a one-to-one correspondence between a region of a displaysurface and a model,and
knows how to render the contents of a modelto the display surface. When the model
changes, the view automatically redrawsthe affected partof the imagetoreflect those
changes.
A controller is the means by which the user interacts with the application. A con-
troller accepts input from the user and instructs the model and view to perform ac-
tionsbasedonthatinput.Ineffect,thecontroller isresponsibleformappingend-user
action to application response. For example, if the user clicks the mouse button or
Search WWH ::




Custom Search