Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

27
Fundamentals of Computer Science The Object-Oriented Paradigm and UML

Transcript of Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Page 1: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Fundamentals of Computer Science

The Object-Oriented Paradigm and UML

Page 2: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Object-Oriented versus Structured Paradigm

The object-oriented paradigm is a response to shortcomings in the structured paradigm. The structured approach does not work as

well for large projects. The structured approach requires that you

focus on the system’s data or actions individually.

The object-oriented approach integrates actions and data so that they are both viewed at the same level of importance.

Page 3: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Objects/Classes are superior because … They have low coupling and high cohesion. Data and actions are packaged together.

• Objects are self-contained.

• Integrity of the data is easier to maintain. They support information hiding.

• They have a well-defined interface.

• Member data is inaccessible outside of object. They are easy to reuse.

• For all the above reasons.

• Inheritance further enhances reusability.

Object-Oriented versus Structured Paradigm

Page 4: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

The Object-Oriented Paradigm

Object-oriented programming is a logical extension of the concept of modularity.

The concept of objects was introduced in 1966.

Object-oriented programming began to gain wide spread acceptance in the mid-80’s.

The object-oriented paradigm facilitates the construction of large systems by promoting high cohesion and low coupling.

Page 5: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

The Object-Oriented Paradigm

The primary benefit of object-oriented design/programming is that it facilitates software reusability. Inheritance plays an important role in software

reusability. The transition from the analysis phase and the

implementation phase is less severe when the object-oriented approach is used. Many of the same diagrams used in the

analysis phase may also be used, with modification, in the design phase.

Page 6: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

The object-oriented approach to software design is generally agreed to be superior to the structured approach; especially for large projects.

The object-oriented paradigm is based on the following four principles: Abstraction Encapsulation Inheritance Polymorphism

For a programming language to be object-oriented it must support the above principles.

The Object-Oriented Paradigm

Page 7: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Abstraction What is an abstraction?

A simplification of reality in which essential features are extracted from a conceptual or real-world entity.

Models are types of abstractions. What are models/abstractions used for?

To represent some physical or conceptual thing that is to be built.

As a plan of attack for solving a problem. As a blueprint of the desired system. As a means of communicating complex ideas to others. As a means of viewing the “big picture”.

Page 8: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Encapsulation

Encapsulation is the act of combining data and the subroutines that act on that data in one entity, usually a class.

The data is, in most cases, only directly accessible to the subroutines encapsulated with the data.

The purpose of the subroutines is to protect the integrity of the accompanying data.

Page 9: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Object-Oriented Paradigm for a Bank Account System

Page 10: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Classes and Objects An objects is an instances of a class.

A class is ... The most important building block of any object-

oriented system. A description of a set of objects that share the

same attributes and behaviors. A blueprint for creating an object. An abstraction (simplification) of reality. A representation of a software thing, a hardware

thing, or even a conceptual thing. Graphically represented in UML as a rectangle.

Page 11: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

All classes have … Names - Used to distinguish one class from

another. A class must have a name. Attributes - Member data. Operations (behaviors) - Member functions.

Attributes and operations are the most important features of a class definition.

A class usually collaborate with other classes.

Classes and Objects

Page 12: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Inheritance There are different types of relationships that can exist

between collaborating classes. Inheritance is …

A type of relationship that can exist between two classes. One of the classes is the base (or parent) class; the other class is the derived (or child) class.

Used to show a ‘kind-of’ relationship. All OO programming languages support single

inheritance; some (C++) also support multiple inheritance.

Graphically represented in UML by a solid line with an open triangular arrowhead on the base class end.

Page 13: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Association

An association is … A type of relationship that can exist between one

or more classes. Used to show a ‘knows-a’ relationship. Either unidirectional or bi-directional. Graphically represented in UML by a solid line

which may optionally be labeled and have a name direction indicator.

Association names are verbs or verb phrases. The same class can be on both ends of a binary

association.

Page 14: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Aggregation

Aggregation is … A relationship between two classes. A form of association. Used to show a ‘has-a’ relationship. Graphically represented in UML as a

solid line with an open diamond on the ‘whole’ end.

Aggregations and associations are implemented in exactly the same way. The difference is entirely conceptual.

Page 15: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Composition

Composition is … A relationship between two classes. A strong form of association. Used to show a ‘contains-a’ or a ‘whole-part’

relationship. Graphically represented in UML as a solid line

with a solid diamond on the ‘whole’ end. Composition relationships and associations are not

implemented in exactly the same way. If the ‘whole’ object is destroyed, the part object

will also be destroyed.

Page 16: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Polymorphism

Polymorphism … Is used in the context of inheritance, where each

class in the inheritance hierarchy has a function (or functions) with the same signature.

Is also known as dynamic binding. Is the technique of deferring until run-time the

decision on the function within the hierarchy to call.

Promotes software reusability. Reduces code complexity.

Page 17: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Class Modeling

How are the classes that comprise a system chosen? From scenarios. From a problem statement. From CRC (Class, Responsibility,

Collaboration) cards.• CRC cards are often derived from scenarios

and problem statements. From past experience working with similar

systems.

Page 18: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Noun extraction is a valuable tool in obtaining an initial list of potential classes. Underline all of the nouns in the problem

statement or scenarios. Refine the list of nouns (potential classes)

• Remove redundant classes• Remove vague classes• Remove things best modeled as attributes.• Remove irrelevant classes.

Class Modeling

Page 19: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Class Modeling - CRC Cards

Page 20: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Overview of the Unified Modeling Language

Page 21: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Overview of UML

UML is a language. Conforms to specific rules. Allows the creation of various models. Does not tell the designer which models need to be

created.

UML is a language for visualizing. UML is a graphical language. Pictures facilitate communication (a picture is worth a

thousand words).

UML is a language for constructing and understanding.

UML supports both forward and reverse engineering.

Page 22: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

UML is a language for documenting design Provides a record of what has been built. Useful for bringing new programmers up to

speed. Useful when developing new releases of product.

UML is intended primarily for software-intensive systems

Overview of UML

Page 23: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

UML Relationship Syntax

Page 24: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

UML Class Icon Syntax

Page 25: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Sequence diagrams ... Are one technique used by UML to model the dynamic

aspects of a system. Emphasize the time ordering of message traffic between

objects. Aid the developer visualize the system as it is running. Are storyboards of selected sequences of message traffic

between objects.

After class diagrams, sequence diagrams are the most widely used diagrams in UML.

Sequence diagrams commonly contain objects and messages.

Dynamic Modeling Using UML

Page 26: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

Sequence diagrams are build around an X-Y axis. Objects are aligned (in most cases) at the top of the

diagram, parallel to the X axis. Messages travel (in most cases) parallel to the X axis. Time passes from top to bottom along the Y axis. Where a message arrow appears relative to the Y axis

and other message arrows, determines the relative time the message is sent.

Sequence diagrams most commonly show relative timings, not absolute timings.

Links between objects are implied by the existence of a message.

Sequence Diagrams

Page 27: Fundamentals of Computer Science The Object-Oriented Paradigm and UML.

UML Sequence Diagram Syntax