Software Product Line Engineering
Download
Report
Transcript Software Product Line Engineering
Object-Oriented Programming
Languages
Principles of Object-Oriented
Software Development
(Chapter 5)
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 1
Objective
To illustrate the concepts of object-oriented
programming languages in a broader context
beyond the familiar Java and C++ interpretations.
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 2
Outline
The object paradigm
Comparison: Smalltalk, Eiffel, C++, Java
Dimensions of language design
Classless languages
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 3
The Notion of Object
The notion of objects appears in many areas of
computer science
•
•
•
•
Software engineering: Abstract data types
Artificial intelligence: Frames
Databases: Semantic data models
Distributed systems: Capability-based computing
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 4
Perspectives on
Object-Orientation
Structural
•
•
Operational
•
•
Capability of representing arbitrarily structures complex objects
An object is a data structure in memory
The ability to operate on complex objects through generic
operators
An object represents an element of a conceptual model
Behavioral
•
•
Harvey Siy
The specification of types and operations
An object is a type, used for data abstraction
Principles of Object-Oriented Software Development by Eliens
Slide 5
Characteristics of ObjectOriented Languages
Object creation facility
•
Message-passing capability
•
Must be able to communicate between objects
Class capability
•
Must be able to instantiate objects
Must have a mechanism for defining classes
Inheritance features
•
Harvey Siy
Must support some form of inheritance
Principles of Object-Oriented Software Development by Eliens
Slide 6
Classifications of Object-Oriented
Languages
Hybrid – extensions to existing languages
•
•
O-O retrofitted to existing languages: C, Lisp, Pascal,
Prolog, etc
Examples: C++, CLOS, Objective Pascal, DLP
Frame-based – knowledge-based reasoning
•
•
•
Frame – a structure consisting of slots
Slot – a value of an attribute or a relation to other
frames
Examples: KRL, LOOPS
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 7
Classifications (continued)
Distributed, concurrent, actor – parallel computing
•
•
•
Active object – executes in parallel with other active objects
Examples: Concurrent Smalltalk, sC++, POOL-T
Actor languages
• Instead of threads, parallel execution is realized by selfreplacement
• Actor processes a message sent to it, and creates a successor
object in its place, thus the same actor potentially takes on a
different identity
Alternative object models
•
•
•
Harvey Siy
Removes the distinction between classes and objects
Everything is an object
Examples: Self
Principles of Object-Oriented Software Development by Eliens
Slide 8
Object-Oriented Scripting Languages
Many scripting languages are designed to
support object-oriented programming
•
•
Built-in support
Embedding an O-O language
Javascript
Perl, JPL (Java-Perl module)
Tcl/Tk, Jacl (Tcl/Java)
Python, JPython
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 9
Objects in Javascript
<script language=Javascript>
function object_display(msg) {
return msg + ' (' + this.variable++ + ')';
}
function myobject() {
this.variable=0;
this.display = object_display;
return this;
}
var a = new myobject();
object method
object constructor
create object
document.write(a.display("a message"));
document.write(a.display("another message"));
</script>
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 10
Outline
The object paradigm
Comparison: Smalltalk, Eiffel, C++, Java
Dimensions of language design
Classless languages
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 11
Comparing Smalltalk, Eiffel, C++
and Java
Criteria
•
Class libraries
• Availability of sufficient class library support
•
Programming environment
• Availability of environment to support development
•
Harvey Siy
Language characteristics
Principles of Object-Oriented Software Development by Eliens
Slide 12
Smalltalk Example
Behavior subclass: #Ctr
instanceVariableNames: 'value'
Ctr methodsFor: 'initialization'
initialize
value := 0.
Ctr methodsFor: 'modifications'
add: aValue
value := value + aValue.
Ctr methodsFor: 'inspection'
value
^value
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 13
Eiffel Example
class counter export inc val feature
count : Integer
create is do count := 0 end
inc( n : Integer ) is
require n > 0 do
count := count + n
ensure count = old count + n
end
val : Integer is do Result := count end
invariant count >= 0
end -- class counter
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 14
C++ Example
class ctr {
public:
ctr() { n = 0; }
~ctr() { cout << "bye"; };
void add( int i = 1) { n = n + i; }
int val( ) { return n; }
private:
int n;
};
// constructor
// destructor
// Usage:
ctr c; c.add(1); cout << c.val();
ctr* p = new ctr(); c->add(1); cout << c->val();
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 15
Class Libraries
Availability of sufficient class library support
Smalltalk: part of language definition
Eiffel: part of language definition
C++: STL, large number of 3rd party libraries
Java: large number of standardized APIs
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 16
Programming Environment
What constitutes “good” programming
environment?
•
•
Graphical interface for novices
Command line interface for experts
Smalltalk: part of language definition
Eiffel: part of language definition
C++: large number of commercial environments
Java: many popular IDEs (Eclipse, JBuilder,
Visual J#, JDeveloper, NetBeans)
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 17
Language Characteristics
Uniformity of data structures
Documentation value
Reliability
Inheritance mechanism
Efficiency
Memory management
Language complexity
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 18
Uniformity
Uniformity of treatment of data types
Smalltalk
•
Eiffel
•
Elementary data types are distinct from classes
C++
•
Every data type is a class
Elementary data types are distinct from classes
Java
•
Harvey Siy
Elementary data types are distinct from classes
Principles of Object-Oriented Software Development by Eliens
Slide 20
Documentation Value
How easy is it to read a program and to write a correct
program?
Smalltalk
•
Eiffel
•
Special keywords to formally specify correctness of programs
and specify interfaces
C++
•
•
A consistent style in writing programs because everything is a
class
No constructs to support documentation
Terse style is preferred by some over the more verbose
languages
Java
•
Harvey Siy
Javadoc sets a standard for documentation
Principles of Object-Oriented Software Development by Eliens
Slide 21
Reliability
Smalltalk
•
Eiffel
•
Static type checking, correctness assertions
C++
•
•
•
Dynamically typed, no type checking
Inherits unreliability perception from C
Static type checking within a compilation module, weak support
across modules
Consistent type system
Java
•
Harvey Siy
Less error-prone than C++ due to absence of pointers and builtin garbage collection
Principles of Object-Oriented Software Development by Eliens
Slide 22
Inheritance
Smalltalk
•
Eiffel
•
Multiple inheritance
C++
•
Single inheritance
Multiple inheritance
Java
•
•
Harvey Siy
Single inheritance
Multiple interface inheritance
Principles of Object-Oriented Software Development by Eliens
Slide 23
Efficiency
Smalltalk
–
Eiffel
+
–
Compiled
Dynamic binding for all methods
C++
+
+
–
Interpreted
Compiled
Inline functions, flexible memory management, friends
No garbage collection
Java
–
Harvey Siy
Compiled to bytecode; efficiency depends on JVM
Principles of Object-Oriented Software Development by Eliens
Slide 24
Outline
The object paradigm
Comparison: Smalltalk, Eiffel, C++, Java
Dimensions of language design
Classless languages
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 26
Design of Object-Oriented
Languages
Object-oriented languages support the following:
•
•
•
•
•
Object – state + operations
Class – template for object creation
Inheritance – sharing parts of a description
Data abstraction – state accessible by operations
Strong typing – compile time checking
Object-oriented = objects + classes + inheritance
Object-based = objects + classes
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 27
Orthogonal Dimensions
More formally, object-oriented languages can be defined by the
following orthogonal dimensions:
•
Objects – modular computing agents
• Supports construction of modular units to which a principle of locality
applies
•
Types – expression classification
• Types are a more general abstraction than classes
• Dynamic typing – no static type checking, only inability to evaluate an
expression leads to runtime error
• Static typing – compile time determination and checking of the types of all
variables, objects and expressions
•
Delegation – resource sharing
• A mechanism that allows redirection of control dynamically
• A more general mechanism than forwarding a method call
• Examples: single inheritance, multiple inheritance, scope inheritance
•
Abstraction – interface specification
• What is visible and what is hidden to other objects?
• External behavior specified by contracts
• External state specified by public or protected attributes
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 28
Open Systems
A goal of object-oriented language design is
openness
A software system is said to be open if its
behavior can be easily modified and extended
Reactiveness
•
A program has a (runtime) choice between potential
actions
Modularity
•
Harvey Siy
A program can be safely extended (at design time)
by adding new components
Principles of Object-Oriented Software Development by Eliens
Slide 29
Reactiveness
A program has a (runtime) choice between
potential actions
Late binding (polymorphism) provides dynamic
selection of alternatives depending on the
subtype
Guards in concurrent languages provide a
choice for accepting or rejecting a call
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 30
Modularity
A program can be safely extended (at design
time) by adding new components
Languages must provide appropriate information
hiding mechanisms:
•
Hide details of objects from outside world
• Usual notion of encapsulation
• Classes provide modularity by hiding details of the class
from the outside world
•
Hide details of outside world from objects
• Objects should not have to know that other objects are on
the same machine or not
• Objects should not have to know that other objects are
active
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 31
Object-based Concurrency
Object-based concurrency = objects + processes
Approaches
•
Add processes as a primitive data type
• Programmer has the burden of dealing with synchronization
•
Implement active objects
• Objects are simultaneously active
• Language provides constructs to support synchronous
communications (rendezvous)
• Active object interrupts itself to respond to messages
• Potential for deadlock exists with self-invocation
• Performance issues if only a few active objects needed
• Need careful choice in determining which objects should be active
•
Use asynchronous communication through message buffers
• Instead of interrupting, queue up the messages in a buffer
• Real-time constraints become impossible to guarantee
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 32
Outline
The object paradigm
Comparison: Smalltalk, Eiffel, C++, Java
Dimensions of language design
Classless languages
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 33
Classless Languages
Classical object model
•
Objects are instances of classes
Object-oriented = objects + classes + inheritance
Classless languages
•
•
There are no classes, only objects
New objects are created by cloning existing objects
• Objects are cloned from objects called “prototypes”
•
Inheritance is approximated by delegation
• Object chooses which object to designate as parent
• Dynamic binding is implemented by searching up the parent list
•
Advantages
• Conceptually easier to create objects from existing examples rather
than defining a characterization of the object through a class
• Dynamic sharing of code and information is more flexible than
inheritance and instantiation
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 34
Design Issues for Prototypes
State
•
•
Creation
•
•
Shallow cloning – copy the object
Deep cloning – copy the object and all referenced objects
Delegation
•
•
Object consists of slots
Object consists of variables and methods
Implicit delegation – follow the parent chain
Explicit delegation – object is named
Self language: slots, shallow cloning, implicit delegation
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 35
Improving Performance
In pure object-oriented languages, dynamic type checking
and dynamic binding is the rule
•
•
Increases flexibility
Performance suffers
Solutions
•
•
Special-purpose hardware
Hybrid languages
• Create a hybrid language with a procedural language
• Dealing with unwanted interactions can be a complex problem
•
Static typing
• Bounds the language flexibility
•
Dynamic compilation
• Perform partial evaluation, lazy compilation, message splitting
Harvey Siy
Principles of Object-Oriented Software Development by Eliens
Slide 36
Summary
The object paradigm
•
•
Comparing Smalltalk, Eiffel, C++ and Java
•
•
Notion of object – viewpoints
Classification – object extensions
Tradeoffs between pure and hybrid languages
Flexibility versus performance
Design dimensions of object-oriented languages
•
•
•
•
Harvey Siy
Object-oriented = objects + classes + inheritance
Orthogonal dimensions: objects, types, delegation, abstraction
Open systems are systems that can be easily modified and
extended
Classless languages use cloning and delegation instead of
instantiation and inheritance
Principles of Object-Oriented Software Development by Eliens
Slide 44