Professional Documents
Culture Documents
Background Meta-Model
- 3-
Why objects?
Finally I would like to try to answer the following fair question: How could it happen that a team of two working in the periphery of Europe could hit on programming principles of lasting importance? No doubt a bit of good luck was involved. We were designing a language for simulation modeling, and such models are most easily conceived of in terms of cooperating objects. Our approach, however, was general enough to be applicable to many aspects of system development.
Ole-Johan Dahl: sdm Conference Software Pioneers, Bonn, 2001
- 4-
OO Languages - Milestones
- 5-
Ada Modula-2
A Metamodel of Objects 1
A metamodel of objects
defines elements and relationships which can be used for modeling
Relationships are
uses and inherits
Additionally, rules for modeling are defined Different programming languages may define different metamodels
Prof. Dr. H. Lichter
- 7-
A Metamodel of Objects 2
inherits uses
+Sub +Super
0..* 0..* 0..*
+Server +Client
0..*
redefines
Program Module
Class
defines / redefines
is_instance_of
Method
0..*
triggers execution of
Message
receives
Object
Class Method
Object Method
1..*
accesses 0..*
Attribute
0..*
Class Attribute
Object Attribute
- 8-
Objects Messages
Things are characterized through their behavior and how they can be dealt with Two basic questions:
Which information can be derived? Which actions can be triggered?
Objects
encapsulate related information and behavior define an interface through these can be accessed
- 10 -
- 11 -
Inside an object
An object consists of two parts
Data - which represent the state of the object Methods - which can change the data (and only these!)
- 12 -
Object Interfaces 1
Each method defines a signature, consisting of
a name, the argument objects a result object
Signature
results
arguments
Interface
- 13 -
Object Interfaces 2
The interface of an object defines the offered services
Server/Client-Relationship
An object offers services which a client-object uses An object may be a client of one object and a server for another object at the same time Sometimes offered services are implemented by using services of other (associated) objects
( Call-in/call-out interface)
- 14 -
Server/Client-Relationship
Server uses Service Service Associated Object Client uses Server Service
Client
- 15 -
E.g. Objects
a rectangle Data origin = (10, 10) corner = (100, 400) Methods moveTo (p: Point) center : Point area : Integer Data x= 10 y = 10 Methods + (p : Point) : Point putX (i : Integer) putY (i : Integer) a dot
- 16 -
Create is do p.Create; -- create a dot p p.putX (10); -- send p a message p.putY (10); r.Create; -- create a rectangle r ... b := r.contains (p); r.moveTo(p); p.Forget -- destroy dot p
end;
- 17 -
Messages
Objects communicate by sending and receiving messages Objects react to messages by executing a method The receiving object is responsible for
understanding the message execution of the associated method (and its results)
receiver
message name
parameter
r.contains (p)
- 18 -
Classes Inheritance
giro account
fixed deposit
- 20 -
Only the name and the public methods are visible From a class an unlimited number of objects can be derived (instantiated)
- 21 -
Objects of a class
perform the same set of methods have different states
Basically
a class is an Abstract Data Type an object is an Data Capsule Object oriented concepts extend the principle of Information Hiding
- 22 -
Inheritance 1
Inheritance
A basic feature of object oriented languages not found in other paradigms Inheritance allows hierarchical class structures
Technical implications
All aspects of a superclass are valid for its subclasses also
the unit of inheritance is the class
Subclasses may specialize or extend aspects of their superclass The superclasses relationships are statically defined, these relationships are typically unchanged during runtime.
- 23 -
Inheritance 2
Common features of different classes
are collected in a own superclass are now inherited from this superclass
Rule
Class A inherits from class B if and only if A is a specialization of B
Geo_Object
both have common aspects, E.g. area, extent
E.g.
Rectangle
Circle
- 24 -
Types of Inheritance
no of superclasses means of modification
one / none
unlimited
extension / definition
- 25 -
Examples 1
feature center : Point; class Geo_Object radius : Integer: feature moveTo: (p : Point) is deferred end; contains (p : Point) : Boolean is deferred end; end -- class Geo_Object ... end -- class Circle class Rectangle inherit Geo_Object define moveTo, contains
- 26 -
Examples 2
class Geo_Object feature moveTo: (p : Point) is deferred end; contains (p : Point) : Boolean is deferred end; end -- class Geo_Object
class Rectangle inherit Geo_Object define moveTo, contains feature origin, corner : Point; ... end -- class Rectangle
class DisplayableObject feature psDescription : String is deferred end; end -- class DisplayableObject
class DisplayableRectangle inherit Rectangle inherit DispayableObject define psDescription feature ... end -- class DisplayableRectangle
Multiple inheritance
- 27 -
Examples 3
Simple Inheritance
owner number balance interest rate
account
giro account
fixed deposit
owner: Horst Lichter number: 0815 balance: $1000 interest rare: 0,5 % personal drawing credit: $1000 credit card: visa 3249-3458-...
0815
0816
4711
4712
- 28 -
Examples 4
code
telebanking Object
account
giro account
fixed deposit
Multiple Inheritance
0815
Prof. Dr. H. Lichter
0816
4711
4712
- 29 -
Abstract classes
A class is not necessarily a complete implementation
All methods of concrete classes are executable Some of the methods of abstract (virtual) classes are not yet implemented (only specified)
Concrete classes
can have unlimited instances (by receiving a create-message)
Abstract classes
must not have any instances
- 30 -
If a method only implements a standard behavior, there is a slot for the subclasses to extend this behavior (hook)
Template methods
Template methods implement a complete algorithm by abstract methods
It can only run when the concrete methods are implemented in subclasses
Basic methods
Basic methods are fully executable methods of a abstract class (i.e. completely implemented)
- 31 -
- 32 -
public void clear() { /* Removes all of the elements from this collection. This collection will be empty after this method returns unless it throws an exception. */ Iterator iter = this.iterator(); while (iter.hasNext()) { iter.next(); iter.remove(); } }
- 33 -
Extension
add new aspects (data or methods)
Redefinition
overwrite existing methods
Definition
implement abstract methods
- 34 -
Example - Extension
class Geo_Object feature moveTo: (p : Point) is deferred end; contains (p : Point) : Boolean is deferred end; end -- class Geo_Object
class Rectangle inherit Geo_Object define moveTo, contains feature origin, corner : Point; feature height : Integer is do ... end; width : Integer is do ... end; ... end -- class Rectangle
- 35 -
Example - Definition
class Geo_Object feature moveTo: (p : Point) is deferred end; contains (p : Point) : Boolean is deferred end; end -- class Geo_Object
class Rectangle inherit Geo_Object define moveTo, contains feature origin, corner : Point; feature moveTo: (p : Point) is do origin := origin + p; corner := corner + p; end; contains (p : Point) : Boolean is do ... end; ... end -- class Rectangle
- 36 -
Example - Redefinition
class DisplayableObject feature psDescription: String is deferred end; end -- class DisplayableObject
class Rectangle ... feature initialize is do origin.Create; corner.Create; origin.initialize; corner.initialize; end ... end -- class Rectangle
class DisplayableRectangle inherit Rectangle rename initialize as initRect redefine initialize ... feature color : Color; feature initialize is do current.initRect; color.create; color.black; end; ... end -- class DisplayableRectangle
- 37 -
Aspects of OO Architectures
Design
Classes for specification for implementation Relationships between classes use inherit
Code
(Static)
Run-time
(Dynamic)
Objects Instances of the classes Relationships between objects Send and receive messages
- 38 -
Summary
Central modeling concepts
objects classes inheritance
Classes model concepts of the domain Inheritance is used to model generalization / specialization This leads to abstract classes Subclasses can extent, redefine, define methods of superclasses
- 39 -