Graphics Reference
In-Depth Information
Gammaetal.( )discusses “designpatterns”asapracticalcatalogforwriting
good programs using basic ideas of OOP such as inheritance, polymorphism, and
encapsulation.
Asthebookwaswrittenbyfourauthors, theyarecalled the“Gang ofFour”(GoF),
and their design patterns are sometimes called the “GoF design patterns.” he
patterns are divided into three categories: creational, structural, and behavioral.
Creational patterns proposethe bestwaytocreate instances ofobjects. Using sim-
plythe new methodtocreateaninstance isnotappropriateinmanycases.Creational
patterns abstract the creation process into a special creator class.
Structural patterns describe how classes and objects can be combined to form
larger structures. Patterns for classes describe how inheritance can be used to pro-
vide more useful program interfaces. Patterns for objects describe how objects can
be composed into larger structures using object composition.
Behavioral patterns are concerned with communication between objects.
Some of these design patterns are illustrated individually in the next section
with statistical graphics examples.
Roughlyspeaking,thesedesignpatternsdonotrecommendtheuseofinheritance.
Instead, they recommend the useof an interface to realize polymorphism,and touse
composition to reuse existing classes. hese are quite different from the historical
OOP approaches. hese ideas are mainly based on several basic principles (Martin,
).
The Open-Closed Principle
his principle states that classes should be open for extension, but closed for modifi-
cation. In other words, it should be possible to extend the functions of a class, but it
should not be possible to modify source code.
If we can extend or change the functions of a class by just adding new source code
without checking old source code, the class is designed correctly according to this
principle.
The Dependency Inversion Principle
hisprincipleinsiststhathigh-levelmodulesshouldnotdependuponlow-levelmod-
ules,andbothshoulddependuponabstractions.Italsoinsiststhatabstractionsshould
not depend upon details and details should depend upon abstractions.
InJava,abstractions arerealized byinterfaces andabstract classes.hus,this prin-
ciple recommends that we write programs by using the methods defined in them.
Liskov Substitution Principle
his principle insists that every method that operates on a superclass should be able
tooperateonsubclasseswithoutknowingthem.Inotherwords,anymethodthatuses
a superclass must not be confused when a subclass is substituted for the superclass.
If this principle is violated, a method that operates on a variable that points to an
object must first check the type of the actual object in order to work correctly.
Search WWH ::




Custom Search