CSSE 501 Object-Oriented Development

advertisement
CSSE 501 Object-Oriented
Development
Course Overview
 Course Goals
 Fundamentals and principles of objectoriented development
 Object-oriented analysis, design and
Programming
Assumptions
 Familiar with preliminary
programming background
 Data types
 Program control structures: sequential.
selection, repetition
 Streams and files (program input and
output)
 Simple data structures: array
 Compilation and execution of simple
programs
An Example (C++)
// File name: HelloWorld.cpp
// Purpose: A simple C++ program which prints "Hello World!" on the screen
#include <iostream> // need this header file to support the C++ I/O system using
namespace std;
// telling the compiler to use namespace "std",
// where the entire C++ library is declared.
int main() {
// Print out a sentence on the screen.
// "<<" causes the expression on its right to
// be directed to the device on its left.
// "cout" is the standard output device -- the screen.
cout << "Hello World!" << endl;
return 0; // returns 0,which indicate the successful
// termination of the "main" function
}
If not…
 Decide which programming language
you are going to use in this course,
and pick up a book, start to read it
and practice programming
 No time and material in this course
will be teaching you those preliminary
programming experience
Course Overview
 Topics
 Abstraction, Encapsulation
 Interface and Implementation, Abstract
Data Type
 Object-Oriented Design Principles
 Classes, Methods, Instances
 Object Communication (Message
Passing)
 Inheritance, Multiple Inheritance
Course Overview
 Topics (Cont.)
 Static and Dynamic Behavior
 Polymorphism
 Overriding, Overloading, Polymorphic
Variable
 Object Interconnection
 Coupling and Cohesion
 Reflection and Retrospection
 Exception, Error Handling
Course Overview
 Course Website

http://fac-staff.seattleu.edu/daia/web/teaching/fall07/csse501/csse501.htm
 Instructor
 Lirong Dai, Ph.D., University of Texas at Dallas
 Course Duties:





Material Reading
In-Class Exercises/Lab
Programming Assignments
Midterm
Final
Today…
 Chapter 1: Thinking Object-Oriented
 Chapter 2: Abstraction
Chapter 1: Thinking ObjectOriented
 Why Object-Oriented?
 Comparison with non-OO approaches
 What is Object-Oriented?
Programming
 Programming a computer involves
writing instructions that enable a
computer to carry out a single task or
a group of tasks
 These set of instructions are called as
programs or software
 Programming languages: Java, C++,
C#…
An Example
// File name: HelloWorld.cpp
// Purpose: A simple C++ program which prints "Hello World!" on the screen
#include <iostream> // need this header file to support the C++ I/O system using
namespace std;
// telling the compiler to use namespace "std",
// where the entire C++ library is declared.
int main() {
// Print out a sentence on the screen.
// "<<" causes the expression on its right to
// be directed to the device on its left.
// "cout" is the standard output device -- the screen.
cout << "Hello World!" << endl;
return 0; // returns 0,which indicate the successful
// termination of the "main" function
}
A Survey of Programming
Techniques/Paradigm




Unstructured Programming
Procedural Programming
Modular Programming
Object-Oriented Programming
Unstructured Programming
 All code is contained in a single continuous
block (e.g., “main” program)
 Has to rely on flow execution statements,
such as GOTO (e.g., Spaghetti code)
 Still used in
 MS-DOS batch files
 Old languages, such as BASIC, FORTRAN
 Assembly language
Program
Main Program
data
MS-DOS Batch File Example
rem display and check if there is an argument
if "%1"=="" goto noparameter
echo the parameter is %1
goto exit
:noparameter
echo you must supply an argument to this batch file
:exit
Unstructured Programming (Cont.)
 Problems
 Difficult to read and debug
 Only works for very simple and small problems
 Tremendous disadvantages once the program
gets sufficiently large
 For example, if the same statement sequence is
needed at different locations within the
program, the sequence must be copied
 Solution: extract these sequences, name them
and offer a technique to call and return from
these procedures
Procedural Programming
 Able to combine returning
sequences of statements into
one single place
 A procedure call is used to
invoke the procedure
 Data from main program to
procedures (i.e., parameters)
 After the sequence is processed,
flow of control proceeds right
after the position where the call
was made
 Processed data back to main
program
 Procedures, also known as
routines, subroutines, methods,
or functions
Main
Program
Procedure
Procedure calls
C Program Example
int add( int, int); /* Function declaration */
main()
{
int i=1;
printf("i starts out life as %d.", i);
i = add(1, 1); /* Procure/Function call */
printf(" And becomes %d after function is executed.\n", i);
}
/********************************************************/
int add( int a, int b) /* Procure/Function definition */
{
int c;
c = a + b;
return c;
}
Procedural Programming (Cont.)
 Languages
 COBOL, FORTRAN, BASIC, C, Pascal …
 Problems
 Difficulties of reasoning about programs
 To some degree difficulty of
parallelization
 Relatively low level
 In large, complicated programs,
modularity is generally desirable
Modular Programming
 Module
 Generally, a component of a
larger system, and operates
within that system
independently from the
operations of the other
components
 Modular Programming
 A common functionality are
grouped together into
separate modules
 A program is now divided
into several smaller parts
which interact through
procedure calls
Main Program
data
Module1
data
+data1
Procedure1
Module2
data
+data2
Procedure2
Procedure3
Modular Programming (Cont.)

Example: a singly linked list

Operations on the list


Append element in the end
Delete element at the front

Application: queue

Implement the list in two separate modules



Interface definition
 Describe what is available
Implementation files
 Describe how it is made available
Fundamental principles in software engineering
 Change the implementation won’t affect the interface
Singly List Interface Definition
/* * Interface definition for a module which implements *
a singly linked list for storing data of any type. */
MODULE Singly-Linked-List-1
BOOL list_initialize(); /* Initialize variables local to the module
BOOL list_append(ANY data);
BOOL list_delete();
list_end();
ANY list_getFirst();
ANY list_getNext();
BOOL list_isEmpty();
END Singly-Linked-List-1
Singly List (Cont.)
 What if we need more than one list in the program?
/* * A list module for more than one list. */
MODULE Singly-Linked-List-2
DECLARE TYPE list_handle_t; /* list_handle_t represents list handle, which is
used in every provided procedure to uniquely
identify the list in question */
list_handle_t list_create();
list_destroy(list_handle_t this);
BOOL list_append(list_handle_t this, ANY data);
ANY list_getFirst(list_handle_t this);
ANY list_getNext(list_handle_t this);
BOOL list_isEmpty(list_handle_t this);
END Singly-Linked-List-2;
Singly List (Cont.)
PROCEDURE foo()
BEGIN
list_handle_t myList;
myList <- list_create();
/* Do something with myList */
...
list_destroy(myList);
END
PROCEDURE foo()
BEGIN
list_handle_t myList; /* List is created and initialized */
/* Do something with the myList */
...
END /* myList is destroyed */
Singly List (Cont.)
PROCEDURE foo()
BEGIN
SomeDataType data1;
SomeOtherType data2;
list_handle_t myList;
myList <- list_create();
list_append(myList, data1);
list_append(myList, data2); /* Oops */ ...
list_destroy(myList);
END
Modular Programming
 Problems
 Decouple data and behaviors
 Module is oriented on operations but not
actual data
 Cannot guarantee type safety
 Programs can access memory in
inappropriate ways
 Inflexible
 Hard to extend
Object-Oriented Programming
(OOP)
 What is object-oriented programming?
 Principles of object-oriented programming
Kay's Description of OOP
 Object-oriented programming is based on
the principle of recursive design
 Everything is an object
 Objects perform computation by making requests of
each other through the passing of messages
 Every object has it's own memory, which consists of
other objects
 Every object is an instance of a class. A class groups
similar objects
 The class is the repository for behavior associated
with an object
 Classes are organized into singly-rooted tree
structure, called an inheritance hierarchy
What is an Object?
 The most fundamental concept/mechanism
of OOP
 From an application modeling perspective, an
object has the components:
characteristics/attributes, services/behaviors),
unique identifier, rules and policies, relationships
 From a design modeling perspective, an object
can be an example (instance) of a category
(class), can be a category or a type, created
(instantiated) by a category, can communicate…
What is a Class?
 From a modeling perspective, a class is a
template for a category. It defines
characteristics, services, rules and policies,
relationships
 From a design perspective, a class is a
special kind of object
 From an implementation perspective, a
class is a “global” object with class data
members and class services
 From a compiler’s perspective, a class is a
programmer’s defined data type
Principles of Object-Orientation
 Principle 1. Encapsulation: The
object contains both the data and the
methods (code) that manipulate or
change that data
Case Study
In this example, we have a family with a father (John), a mother (Jane), two
sons (Peter and Paul), and two daughters (Elizabeth and Mary). John is an actor
and Jane is a dentist. All the children are students and the family dog is Lassie.
Their family physician is Alice. This family owns a house in the suburbs. Although
mowing the family lawn is normally a chore for the father, it can be a paid chore
for any one of the children. However, working within the neighborhood is Jack, a
professional lawn mower.
One morning, Jane notices that the lawn needs mowing, so she mentions to
John that it is time to mow the lawn. John agrees and says that he will mow the
lawn this evening. Later that evening, John comes home and is exhausted from a
long day at the studio and decides to pay one of his children to mow the lawn. He
looks for one of his children; he sees Mary first. He asks Mary to mow the lawn
for five dollars. Mary agrees; however, Mary knows that Jack, a professional lawn
mower, is willing to mow the lawn for four dollars. So Mary calls Jack to mow the
lawn for four dollars and Jack agrees to mow the lawn. Jane comes home later
that evening and she sees the lawn mowed. Thinking that John mowed the lawn,
Jane complements John on the excellent condition of the lawn.
Principles of Object-Orientation



Principle 2. Information Hiding: The object that contains
the attributes (data) defines what services (functions) are
available to the other objects and prevents other objects from
access or knowledge of the attributes (data) and how a service
(function) is provided
Information hiding is important because it allows an object
complete control over the integrity of the data contained within
it. This gives us high cohesion and low coupling concerning
the manipulation of data
Note: Encapsulation alone does not prevent the data of an
object from being manipulated by functions other than the
methods bound to the object. Protecting data from
manipulation by entities outside the object can be achieved by
requiring that access of data can only be provided by the
services of the object that contains the data.
Principles of Object-Orientation
 Principle 3. Message Passing: An object
may communicate with another object only via
the message-passing mechanism
 Each message must be sent to a designated
receiver, and the interpretation of the message
depends on the receiver
Principles of Object-Orientation
 Principle 4. Late Binding: Support for the ability to
determine the specific receiver and its corresponding
method (code) to be executed for a message at runtime
 This is another way in which message passing in the OOP
differs from a function call is that the specific receiver of
any given message is not usually known until runtime,
so the determination of which method to invoke cannot
be made until then

Late binding enables to model a real behavior of the world.
For example, if you are reading the textbook in a class at
school, it is unlikely that you could have known who your
classmates would be before the first day of the class. You
only know who your classmates are once the class has
begun, and even then some individuals might register late
Principles of Object-Orientation
 Principle 5. Delegation: Work is passed, via
message passing, from one object (client) to
another object (agent) because from the
client’s perspective, the agent has the service
that the client needs. Work is continuously
passed until it reaches the object that has both
the data and the method (code) to perform the
work
Principles of Object-Orientation
 Principle 6. Class/Instance/Object: All
objects are instances of a class. Instance can
be created (instantiated) or destroyed
(deleted) at runtime. How the object provides
a service is determined by the class of which
the object is an instance
Principles of Object-Orientation
 Principle 7. Generalization/Specialization:
Classes can be organized by using a
hierarchical inheritance structure. In the
structure, the specialized class (subclass)
inherits the attributes, the relationships, the
methods from the generalized class
(superclass) that is higher in the structure
Principles of Object-Orientation
 Principle 8. Relationships: Collaboration
between objects to provide a service to a client
are usually captured by an association
relationship
Principles of Object-Orientation
 Principle 9. Reflection: Each object knows
the detailed information about the class(es)
and the interface(s) to which it is an instance.
This means that an application can, at runtime,
acquire information about the object from the
object itself
Download