Development of Component-and Service-Oriented … · · 2007-02-23abstraction in OO • A class...
Transcript of Development of Component-and Service-Oriented … · · 2007-02-23abstraction in OO • A class...
California Institute for Telecommunications and Information TechnologiesLa Jolla, CA 92093-0405, USA
Department of Computer Science & EngineeringUniversity of California, San DiegoLa Jolla, CA 92093-0404, USA
Development of Component- and Service-Oriented Software Architectures with UML and UML-RT
— Objects and Classes —
Ingolf H. [email protected]
© Ingolf H. Krueger 2February 07, 2007 CSE
• Objects
• Classes
• Class and Object Diagrams
• Objects
• Classes
• Class and Object Diagrams
Overview
© Ingolf H. Krueger 3February 07, 2007 CSE
Advantages of Object-Orientation
• Seamless methodological approach covering analysis to implementation
– OO modeling techniques (classes/objects)
– OO implementation languages (C++, Java, Smalltalk, ...)
• Support for understanding the application domain
– Mapping of “real” entities to objects of the model
– Abstraction mechanisms available
• Starting points for reuse of structure and behavior:
– Classes,
– Interfaces,
– Objects/components
© Ingolf H. Krueger 4February 07, 2007 CSE
Advantages of Object-Orientation
• Adequate even for modeling of large systems
• Support for developing “safe” components:
– Interfaces
– Encapsulation of data structures and behavior
• Support for distribution/parallelism
– Objects as independent and active entities executing in parallel
© Ingolf H. Krueger 5February 07, 2007 CSE
Objects
• Objects model (individual) entities of the application domain
• Example (telecommunication):
– Caller
– Connection
– Account
– ...
• Objects have unique identity
• Objects encapsulate
– Data/state
– Behavior
© Ingolf H. Krueger 6February 07, 2007 CSE
Objects
• Access only via explicit message exchange
⇒Integrity of data
• Operation/Method: enable specification of pre-/postconditions
© Ingolf H. Krueger 7February 07, 2007 CSE
Example: Stack-Object
int [10] stack
void push(int)
int pop()
Attribute(s):
Encapsulated data
Operations
Object
© Ingolf H. Krueger 8February 07, 2007 CSE
push(2)
Example: Stack-Object
int [10] stack
void push(int)
int pop()
2
© Ingolf H. Krueger 9February 07, 2007 CSE
int [10] stack
void push(int)
int pop()
2
push(3)
Example: Stack-Object
32
© Ingolf H. Krueger 10February 07, 2007 CSE
Example: Stack-Object
int [10] stack
void push(int)
int pop()
pop() 3
322
© Ingolf H. Krueger 11February 07, 2007 CSE
int [10] stack
void push(int)
int pop()
2
push(5)
Example: Stack-Object
52
© Ingolf H. Krueger 12February 07, 2007 CSE
int [10] stack
void push(int)
int pop()
52
Example: Stack-Object
pop() 5
2
© Ingolf H. Krueger 13February 07, 2007 CSE
int [10] stack
void push(int)
int pop()
Example: Stack-Object
pop() 2
2
© Ingolf H. Krueger 14February 07, 2007 CSE
Example: Stack-Operations
• Operation “push(arg)”:
– only allowed, when stack not full (otherwise: exception handling)
– arg becomes new top element of stack,
– rest of stack remains unchanged
• Operation “pop()”:
– only allowed, when stack not empty (otherwise: exception handling)
– top stack element is returned, and removed from stack,
– rest of stack remains unchanged
Precondition
Precondition
Postcondition
Postcondition
© Ingolf H. Krueger 15February 07, 2007 CSE
Cabin Cabin
Cabin
Example: Elevator Controller
Cabin
Door User Console
ButtonButtonButton Lock
Position Display
Elevator Control
Scheduling strategy...
Object
Relationship
...
© Ingolf H. Krueger 16February 07, 2007 CSE
Cabin Cabin
Cabin
Example: Elevator Controller
Cabin
Door User Console
ButtonButtonButton Lock
Position Display
Scheduling strategy...
Object
Relationship
...
State:
unlocked/locked/
unlocking/locking
Operations:
unlock
lock
Door
State:
Running/Stopped
Operations:
switch on
switch off
Lock
Elevator Control
© Ingolf H. Krueger 17February 07, 2007 CSE
• Objects
• Classes
• Class and Object Diagrams
Overview
© Ingolf H. Krueger 18February 07, 2007 CSE
Classes
• Objects model (individual) entities of the application domain
• Many objects follow the same “blue-print”Example (elevator):
– Cabins
– Locks
– Buttons
– Scheduling strategies
• “blue-print” consists of
– data-/state-structures
– Patterns of behavior
© Ingolf H. Krueger 19February 07, 2007 CSE
Christina
24 years
married
Teacher
Herbert
56 years
married
Technician
Martin
31 years
unmarried
HR Consultant
Class formation
Classes
• Class in OO approach:
– Description of the similarities (attributes and operations) of a set of objects
– Template for instantiating individual objects
Person
Name
Age
Marital status
Position
set_name(adr)
is_adult()
Class
p1:Person
Christina, 24, m, Teacher
Name
Attributes
Operations/
Methods
p2:Person
Herbert, 56, m,
Technician
p3:Person
Martin, 31, u, HR
consultant
© Ingolf H. Krueger 20February 07, 2007 CSE
Classes
• Classes are one of the key means for structuring and abstraction in OO
• A class models a concept of the application domain / implementation
• Each object is an instance of a classsynonyms: object, instance, class instance, ...
• Classes define templates for individual objects
• Class structure, relations among classes:class diagram
• Object structure, relations among objects: object diagram
© Ingolf H. Krueger 21February 07, 2007 CSE
• Objects
• Classes
• Class and Object Diagrams
Overview
© Ingolf H. Krueger 22February 07, 2007 CSE
Class and Object Diagrams
• Class diagrams:
– Representation of classes and their relationships
– Examples for relationships among classes:
• Existence of a communication path
• Inclusion
• Generalization/Specialization
• Object Diagrams:
– Syntax as in class diagrams
– But: representation of objects instead of classes
© Ingolf H. Krueger 23February 07, 2007 CSE
Class Diagrams: Modeling Elements
• Classes
• Attributes
• Operations
• Association
• Aggregation/Composition
• Generalization/Specialization
• Conditions
Description of “object template”
Description of relationships among multiple classes
Restrictions of the corresponding object model
© Ingolf H. Krueger 24February 07, 2007 CSE
Class Diagrams
Account
account_number
balance
rate_of_interest
credit (x:Real)
debit (x: Real)
close
Person
name
address
birthday
change_adr (adr: String)
adult (): Bool
is_owner_of
**
ClassNameAttributes
Operations/
Methods
Association
Multiplicity
© Ingolf H. Krueger 25February 07, 2007 CSE
Associations
Associations describe navigation paths
through the class/object model
Meaning: There is a link (e.g. communication path) between instances of classes “Cabin” und “User Console”.
Here: navigation in both directions possible
Cabin User Console
General Association Class
© Ingolf H. Krueger 26February 07, 2007 CSE
Associations: Implementation
class Cabin {
User_Console bc = null;
void setCo(User_Console _bc){
bc = _bc;
}...}
Cabin User Console
General Association Class
class User_Console {
Cabin c = null;
void setCa(Cabin _c){
c = _c;
}...}
User_Console console = new User_Console();
Cabin cabin = new Cabin();
console.setCa(cabin); cabin.setCo(console);
© Ingolf H. Krueger 27February 07, 2007 CSE
Other Association Types
directed association
“Cabin” instances can access “User Console” instances, but not the other way around!
Cabin User Console
© Ingolf H. Krueger 28February 07, 2007 CSE
Other Association Types
Person CompanyEmployee Employer
0..11..* works_for
Name of Association
class Company {
private Person[] employees;
...
}
© Ingolf H. Krueger 29February 07, 2007 CSE
Other Association Types
Person CompanyEmployee Employer
0..11..* works_for
“direction” of association
© Ingolf H. Krueger 30February 07, 2007 CSE
Other Association Types
Multiplicity
A Person works for at most one (i.e. “0..1”) Company
A Company employs at least one (i.e. “1..*”) Person
1..*Person Company
Employee Employer
0..1works_for
employs
© Ingolf H. Krueger 31February 07, 2007 CSE
Other Association Types
Person FirmaEmployee Employer
0..11..* works_for
employs
General Format for Multiplicities:
a..b (a, b: natural numbers or “*”)
* = *..* = 0..*
1 = 1..1 (exactly one)
n = n..n (n : natural number)
© Ingolf H. Krueger 32February 07, 2007 CSE
Other Association Types: Aggregation
(weak) Aggregation
Every user console “has” 1-12 buttons and exactly one lock
User Console
Button Lock
1..12
1 1
1
Every lock/button belongs to exactly one user console
© Ingolf H. Krueger 33February 07, 2007 CSE
Other Association Types: Composition
Composition
Every user console contains 1-12 buttons and exactly one lock
User Console
Button Lock
1..12
1 1
1
Every button/lock belongs to exactly one user console
The user console is responsible for construction and destruction of locks and buttons; it controls the “life cycle” of its locks/buttons
© Ingolf H. Krueger 34February 07, 2007 CSE
General Forms of Aggregation
• Constant Aggregation
Every sub-object is part of at most one “container” over the
system‘s runtime
• Dependent Aggregation
No sub-object can exist without a “container”; it must be
part of at least one “container”
• Encapsulated Aggregation
Sub-objects receive messages only from within their
container; there is no communication beyond container
boundaries
• UML:
• Composition = Constant Aggregation + Dependent Aggregation
• Weak Aggregation: No restrictions
© Ingolf H. Krueger 35February 07, 2007 CSE
Inheritance
Inheritance: Generalization/Specialization on class level
Person
Employee
Superclass
Subclass
Inheritance-Relationship
© Ingolf H. Krueger 36February 07, 2007 CSE
Inheritance
Inheritance: Generalization/Specialization on class level
Person
Employee
Attribute Inheritance
MethodInheritance
New Attributes
and Methods
© Ingolf H. Krueger 37February 07, 2007 CSE
Inheritance
• Subclasses inherit attributes, associations and operations from their superclasses
• Subclasses can overwrite inherited operations
• Each instance of a subclass is also an instance of its superclass(Polymorphism)
Graphic_Element
ShapeText Group
© Ingolf H. Krueger 38February 07, 2007 CSE
Polymorphism
Graphic_Element
draw()
Shape
draw()
Text
draw()
Group
draw()
Overwritten
Method
void draw_content(Graphic_Element ge) {
ge.draw();
}
draw_content( new Text() );
draw_content( new Shape() );
draw_content( new Group() );
© Ingolf H. Krueger 39February 07, 2007 CSE
Limited Inheritance
DemoClass
+publicInt:int
#protectedInt:int
-privateInt:int
+publicMethod():void
#protectedMethod():void
-privateMethod():void
Subclass
Public Member:
Accessible in every class definition
© Ingolf H. Krueger 40February 07, 2007 CSE
Limited Inheritance
DemoClass
+publicInt:int
#protectedInt:int
-privateInt:int
+publicMethod():void
#protectedMethod():void
-privateMethod():void
Subclass
Protected Member:
Accessible in DemoClassand all of its subclasses
© Ingolf H. Krueger 41February 07, 2007 CSE
Limited Inheritance
DemoClass
+publicInt:int
#protectedInt:int
-privateInt:int
+publicMethod():void
#protectedMethod():void
-privateMethod():void
Subclass
Private Member:
Accessible only in DemoClass
© Ingolf H. Krueger 42February 07, 2007 CSE
Multiple Inheritance
Land Vehicle Water Vehicle
Amphibic Vehicle
Attention: reserve for abstract classes/interfaces
© Ingolf H. Krueger 43February 07, 2007 CSE
Object Diagrams
• Syntax same as for class diagrams
• Typically no attributes and operations shown
• Object name and class name is underscored
:Cabin
:Door :User Console
:Button :Lock
:Position Display
:Elevator Control
s:Scheduling Strategy
1..* 1
1
11
11
1
1
1..13
1
1
1
1
Anonymous ObjectNamed Object
Class name
© Ingolf H. Krueger 44February 07, 2007 CSE
Summary Class and Object Diagrams
• Class and object diagrams describe classes and objects, respectively, and their relationships:
– intuitively: objects of class A “know” objects of class B (have references to objects of class B)
– Multiplicities, Association names, Role names
– Aggregation/Composition
– Generalization/Specialization
– Polymorphism
• Object diagrams: “snapshot” of running system
• View on system structure
• Direct transformation into implementation