Beruflich Dokumente
Kultur Dokumente
Objects can be used effectively to represent realworld entities For instance, an object might represent a particular employee in a company Each employee object handles the processing and data management related to that employee
Lecture 13
Objects
An object has:
state - descriptive characteristics behaviors - what it can do (or what can be done to it)
The state of a bank account includes its account number and its current balance The behaviors associated with a bank account include the ability to make deposits and withdrawals
Classes
Lecture 13
Lecture 13
Attributes
Contain current state of an object.
Attributes can be classified as simple or complex. Simple attribute can be a primitive type such as integer, string, etc., which takes on literal values. Complex attribute can contain collections and/or references. Reference attribute represents relationship. complex object: contains one or more complex attributes
Lecture 13
Method: Defines behavior of an object, as a set of encapsulated functions Message: Request from one object to another asking second object to execute one of its methods.
(b)
Classes
Class: Blueprint for defining a set of similar objects.
Lecture 13
One class can be used to derive another via inheritance Classes can be organized into hierarchies
Account
Inheritance
Charge Account
Bank Account
Savings Account
Checking Account
Lecture 13
Inheritance (contd)
Inheritance allows one class of objects to be defined as a special case of a more general class. Special cases are subclasses and more general cases are superclasses.
Generalization: process of forming a superclass Specialization: forming a subclass Subclass inherits all properties of its superclass and can define its own unique properties. Subclass can redefine inherited methods. All instances of subclass are instances of superclass. Principle of substitutability: instance of subclass can be used whenever method/construct expects instance of superclass. A KIND OF (AKO): Name for relationship between subclass and superclass
Lecture 13
10
Types of inheritance
(a) (b)
Inheritance (contd)
Define humanBeing to be a class A humanBeing has attributes, such as age, height, gender Assign values to attributes when describing object Define Parent to be a subclass of HumanBeing A Parent has all attributes of a HumanBeing, plus attributes of his/her own (name of oldest child, number of children) A Parent inherits all attributes of humanBeing The property of inheritance is an essential feature of object-oriented languages such as Smalltalk, C++, Ada 95, Java (but not C, FORTRAN)
Lecture 13 12
Inheritance (contd)
Java implementation
Lecture 13
14
Lecture 13
15
Aggregation
UML Notation
Lecture 13 16
Association
UML Notation
Lecture 13
17
Classical paradigm
record_1.field_2
thisObject.attributeB thisObject.methodC()
Object-oriented paradigm
Lecture 13
18
Design of an operating system for a large mainframe computer. It has been decided that batch jobs submitted to the computer will be classified as high priority, medium priority, or low priority. There must be three queues for incoming batch jobs, one for each job type. When a job is submitted by a user, the job is added to the appropriate queue, and when the operating system decides that a job is ready to be run, it is removed from its queue and memory is allocated to it
Design 1 (Next slide) Low cohesionoperations on job queues are spread all over product
Lecture 13 19
Lecture 13
20
Lecture 13
21
Data Encapsulation
Data structure (job_queue) together with operations performed on that data structure
Advantages
Development Maintenance
Lecture 13
22
Data structure
job_queue
initialize_job_queue
add_job_to_queue delete_job_from_queue
Abstraction
Lecture 13
23
Stepwise Refinement
Step 1: Design in terms of high level concepts It is irrelevant how job queues are implemented Step 2: Design low level components Totally ignore what use will be made of them In 1st step, assume existence of lower level Concern is the behavior of the data structure job_queue In 2nd step, ignore existence of high level Concern is the implementation of that behavior In a larger product, there will be many levels of abstraction
Lecture 13
24
Identify aspects of product likely to change Design product so as to minimize the effects of change
Data structures are unlikely to change Implementation may change
Lecture 13
25
Lecture 13
26
Implementation of queueHandler
C++
CS540 Software Design Lecture 13
Java
27
What happens if queue is now implemented as a two-way linked list of JobRecord? Module that uses JobRecord need not be changed at all, merely recompiled
C++
Java
CS540 Software Design Lecture 13 28
Need
Lecture 13
29
Information Hiding
Data abstraction Designer thinks at level of an ADT Procedural abstraction Define a procedureextend the language Instances of a more general design concept, information hiding Design the modules in way that items likely to change are hidden Future change is localized Changes cannot affect other modules
Lecture 13
31
Lecture 13
32
Classical paradigm
Lecture 13
34
Object-oriented paradigm
Lecture 13 35
Polymorphic
Lecture 13
36
Code is hard to understand if there are multiple possibilities for a specific method Strength and weakness of the object-oriented paradigm
Lecture 13
38
Dynamic Binding: Runtime process of selecting appropriate method based on an objects type.
Example: With list consisting of an arbitrary no. of objects from the Staff hierarchy, we can write: list[i]. print and runtime system will determine which print() method to invoke depending on the objects (sub)type.
Lecture 13
39
No such thing! Object-oriented cohesion and coupling always reduces to classical cohesion The only feature unique to the object-oriented paradigm is inheritance Cohesion has nothing to do with inheritance Two objects with the same functionality have the same cohesion It does not matter if this functionality is inherited or not Similarly, so-called object-oriented coupling always reduces to classical coupling
Lecture 13 40
Inheritance-related
No problem
Classical metrics work just fine But dont mislead others by calling them objectoriented
Lecture 13
41
Advantages of Objects
Same as as advantages of abstract data types Information hiding Data abstraction Procedural abstraction Inheritance provides further data abstraction Easier and less error-prone product development Easier maintenance Objects are more reusable than modules with functional cohesion
Lecture 13 42
Summary
Lecture 13
43