CS 4448 - Fall 1998
Object-Oriented Programming and Design
Talk 8.1
by
Scott Longmore
Packages, Modules and Object Classes
Chapter 5, Programming
Perl 2nd Edition
by
Larry Wall, Tom Christiansen and Randal Schwart
-
Introduction
-
Packages
-
Modules
-
Objects/Classes
-
OO Design
-
Summary
I. Introduction
Perl -
-
Philosophy - Laziness, Impatience,
& Hubris
-
OO Philosophy - Use OO design
where applicable, avoid it where it does not apply
-
Governed more by convention
than syntax
-
Does not enforce OO design,
up to designer
-
Provides tools to create OO
design, however no specific syntax for OO elements
II. Packages
-
Package is a namespace that
keeps variables and methods separate from other packages i.e. modularity
-
Fundamental block for modules
and objects
-
With few exceptions, there are
no global variables in Perl
-
Code always compiled in "Current
Package"
-
"Current Package" determined
by package declaration, initial package is main
-
Package declaration determines
which symbol table is used
-
Symbol table is a hash of package
variable names
-
AutoLoading - if Package subroutine
undefined, AUTOLOAD routine evaluated
-
AutoLoading generally used for
loading a definition or system call
III. Modules
-
Reusable package defined in
a file with the same name ending in .pm extension
-
Similar to a header file in
C/C++
-
Implemented through use/require
command
-
Similar to #include at beginning
of file
-
Module used to export symbols
or object/class definition
-
Modules to not implement access
protection like C++, Ada, etc.
-
Designer responsible for encapsulation
and abstraction in design
IV. Objects/Classes
Objects:
-
Object interface not enforced
-
Object is a referenced "thingy"
which knows its package/class
-
Referenced "thingy" becomes
Object when bless command ties reference to package
-
Blessing occurs within "constructor"
-
No constructor syntax, "new"
common convention
Classes:
-
No class syntax, class is simply
a package
-
Inheritance implemented via
@ISA array
-
@ISA contains parent package(s)
to search methods
-
Method inheritance implemented
only
-
Perl typeless, so instance variable
implemented on the fly
Methods:
-
No method syntax, method is
simply a subroutine
-
Class method - expects class
as 1st arg
-
Instance method - expects object
as 1st arg
-
Dual nature method - differentiates
object/class via ref command
-
2 forms of invocation: method
class/instance list or class/instance->method(list)
-
Parent method invocation via
SUPER::
-
DESTROY destructor method
-
AUTOLOAD method can be used
to interface instance variables without separate methods
V. OO Design
Guidelines:
-
Don't verify class within object
- can break inheritance
-
Use 2 arg form of bless i.e.
bless $self $class
-
Derived class knows about immediate
base class not vice versa.
-
When applicable use, containment
(has-a) relationships instead of inheritance (is-a)
Examples:
-
Instance variables and inheritance
-
Containment (has-a) relationships
-
Overriding base class methods
-
Implementation (uses-a) relationship
-
Code reuse
-
Class and Object context
-
Constructor inheritance
-
Delegation ("passes the buck")
relationship
VI. Summary
-
Designer can establish own convention,
however OO paradigm can easily be broken
Perl OO Design Implementations:
Modularity:
-
Packages establish own variable
and method namespace
Encapsulation and Abstraction:
-
Not implemented, up to designer
Design Hierarchy:
-
Method inheritance implemented
via @ISA
Copyright © University of Colorado. All rights reserved.
Revised: October 28, 1998