Vous êtes sur la page 1sur 38

Dr.

Michael Eichberg
Software Technology Group
Department of Computer Science
Technische Universitt Darmstadt

Introduction to Software Engineering

Object-Oriented Thinking
Object-Oriented Thinking
Class-Responsibility-Collaboration Cards
(A very first glimpse of object-oriented analysis and
design.)
A First Glimpse on OO Analysis and Design.
Object-Oriented Thinking | 3

We want to develop a library for representing vector


graphics
We want to support:
Squares
Circles
42
Triangles
Text
We want to be able to export gures as PDF and SVG
documents
A Class-Responsibility-Collaboration (CRC) Card
Object-Oriented Thinking | 4

other classes
Class Collaborations
<collaborator A>
<classname>
<collaborator B>
Responsibilities ...
<responsibility A>
<responsibility B>
... short eng.
sentences
Class-Responsibility-Collaboration (CRC) cards help to
discuss object-oriented designs.
Object-Oriented Thinking | 5

The class name of an object creates a vocabulary for


discussing the design; you should spent enough time to
nd the right words
Responsibilities identify problems to be solved; a
responsibility serves as a handle for discussing potential
solutions; they are expressed using short verb phrases
each containing an active verb
Collaborators are the objects which will send or
receive messages in the course of satisfying
responsibilities
First, we have to identify potential classes.
Object-Oriented Thinking | 6

We want to develop a library for representing vector


graphics
We want to support:
Squares
Circles
42
Triangles
Text
We want to be able to export gures as PDF and SVG
documents
First, we identify potential classes.
Object-Oriented Thinking | 7

We want to develop a library for representing vector


graphics
We want to support:
Squares
Circles
42
Triangles
Text
We want to be able to export figures as PDF and
SVG documents
We start by
identifying nouns.
For each potential class, we create a CRC card.
Object-Oriented Thinking | 8

Class Collaborations
Class Collaborations
Square
Text
Responsibilities
Responsibilities

Class Collaborations

Triangle Class Collaborations

Responsibilities
Circle

Responsibilities

Setup
For each potential class, we create a CRC card.
Object-Oriented Thinking | 9

Class Collaborations

PDFDocument
Class Collaborations
Responsibilities

Figure

Responsibilities

Class Collaborations

SVGDocument

Responsibilities

Setup
We then identify responsibilities and collaborators.
Object-Oriented Thinking | 10

Class Collaborations
Class Collaborations
Square
Text
Responsibilities
Responsibilities
Maintain the
Maintain the text
squares data

Class Collaborations

Triangle Class Collaborations

Responsibilities
Circle
Maintain the Responsibilities
triangles data Maintain the
circles data
We then identify responsibilities and collaborators.
Object-Oriented Thinking | 11

Class Collaborations

PDFDocument
Class Collaborations
Responsibilities

Figure Create a .pdf
file
Responsibilities
Maintain a list of
shapes
Class Collaborations

SVGDocument

Responsibilities
Create a .svg file
We incrementally refine our cards.
Object-Oriented Thinking | 12

Class Collaborations

PDFDocument
Class Collaborations
Responsibilities
Figure Create ...
Save a figure to a
Responsibilities
.pdf file
Maintain a list of
shapes
Class Collaborations

SVGDocument

Responsibilities
Create ...
Save a figure to a
.svg file
We incrementally refine our cards.
Object-Oriented Thinking | 13

Class Collaborations
PDFDocument
PDFDocBuilder
Class Collaborations
Responsibilities
Figure Save a figure to a
.pdf file
Responsibilities
Maintain a list of
shapes
Class Collaborations
SVGDocument
SVGDocBuilder
Responsibilities
Save a figure to a
.svg file
We then identify responsibilities and collaborators.
Object-Oriented Thinking | 14

Class Collaborations
Figure
PDFDocBuilder
Text
Class Collaborations
Text
Responsibilities Circle
Figure Save a figure to a Triangle
Circle
.pdf file Square
Responsibilities Triangle
Maintain a list of Square
shapes
Class Collaborations
How do we Figure
SVGDocBuilder
support this? Text
Responsibilities Circle
Save a figure to a Triangle
.svg file Square
We then identify responsibilities and collaborators.
Object-Oriented Thinking | 15

Class Collaborations
Class Collaborations
Square
Text
Responsibilities
Responsibilities
Maintain the
Maintain the text
squares data

Class Collaborations

Triangle Class Collaborations

Responsibilities
Circle
Maintain the Responsibilities
triangles data Maintain the
circles data
We then identify responsibilities and collaborators.
Object-Oriented Thinking | 16

Class Collaborations
Shape N/A
Class Collaborations

Square
Responsibilities
Abstract over Responsibilities
Class Collaborations
shapes Maintain the
Circle
squares data
Class
Responsibilities Collaborations

MaintainTthe
riangle
circles data
Class
Responsibilities Collaborations
Maintain theText
triangles data
Responsibilities
Maintain the text
Specific for class-based
languages
We then identify responsibilities and collaborators.
Object-Oriented Thinking | 17

Class Collaborations Class Collaborations


Shape Class N/A Collaborations Color N/A
Color
Square
Responsibilities Responsibilities
Abstract overResponsibilities
Class
Converts between
Collaborations
shapes Maintain the Color color spaces
Circle
squares data
Class
Responsibilities Collaborations

MaintainTthe Color
riangle
circles data
Class
Responsibilities Collaborations
Maintain theText Color
triangles data
Responsibilities
Maintain the text

Object-Oriented Thinking | 18

We stress the importance of creating objects not


to meet mythical future needs, but only under the
demands of the moment.

Kent Beck, Ward Cunningham


A Laboratory For Teaching OO-Thinking
Proceedings of OOPSLA 89; ACM Press
Evolving the Application
Object-Oriented Thinking | 19

We want to support further shapes:


Stars
Bubbles
Arrows
Bzier curves
...
We want to support further le formats:
BMP
JPEG
DXF
...
Evolving the Application
Object-Oriented Thinking | 20

If we follow the chosen path i.e, each XDocBuilder


collaborates with all shapes we end up with this:
Triangle
PDFDocBuilder

DXFDocBuilder

Bzier curve

JPEGDocBuilder

Circle
SVGDocBuilder

BMPDocBuilder
Square
Evolving the Application
Object-Oriented Thinking | 21

If we follow the chosen path - i.e, each ...DocBuilder


collaborates with all shapes - we end up with this:
Triangle
PDFDocBuilder

DXFDocBuilder

Consequences: Bzier curve

- Adding a new shape requires updating all


DocBuilders
JPEGDocBuilder

- Supporting a new file format requires


knowledge of all shapes
Circle
SVGDocBuilder

BMPDocBuilder
Square
An Example SVG file
Object-Oriented Thinking | 22

A circle is
2
3
[...]! represented using
<svg version="1.1" viewBox="183 216 143 148" width="143pt" height="148pt">!
4 [...]! an ellipse.
11 <g> !
12
13
Each ...DocBuilder will
<rect x="185" y="218" width="72" height="69" fill="#00009f"/>!
<rect x="185" y="218" width="72" height="69" stroke="black" .../>!
14 contain quite some
<ellipse cx="270" cy="327.5" rx="36.000072" ry="34.500084" fill="#2d4141"/>!
15 <ellipse cx="270" cy="327.5" rx="36.000072" ry="34.500084" stroke="black" .../>!
16 domain logic.
<path d="M 252 241 L 288 310 L 324 241 Z" fill="#ffeb00"/>!
17 <path d="M 252 241 L 288 310 L 324 241 Z" stroke="black" .../>!
18 </g>!
19 </svg>
No direct
support for
triangles.
We then identify responsibilities and collaborators.
Object-Oriented Thinking | 23

Class Collaborations
Figure
PDFDocBuilder
Text
Class Collaborations
Responsibilities
TextThis solution does not Circle
Figure Save a figure to a Triangle
Circle
facilitate software
.pdf file Square
Responsibilities Triangle
Maintain a list of
evolution!
Square
shapes !
We need to think
Class again Collaborations
about the collaborations Figure
SVGDocBuilder
and responsibilities! Text
Responsibilities Circle
Save a figure to a Triangle
.svg file Square
There are some things to consider.
Object-Oriented Thinking | 24

Class Collaborations
If this list gets too long, its probably
Names do matter!
time to split up the class.
Responsibilities If you have cyclic collaboration
If this list gets too long, its probably dependencies its time to think about
time to split up the class. introducing abstractions.
The responsibilities a class has
should be related.
A new class with new responsibilities and collaborators.
Object-Oriented Thinking | 25

Class Collaborations Class Collaborations


Color Color
Triangle Canvas
Canvas
Class Collaborations
Responsibilities Responsibilities
MaintainSquare
the Color draw text, lines,
triangles data Canvas bzier curves,
Class Collaborations
Responsibilities
draw itself Color ellipses and points
Maintain the
Circle
Canvas
squares data
Class Collaborations
Responsibilities
draw itself Color
Text
Maintain the identification of a
Canvas new artificial class
circles data
Responsibilities
draw itself the text (w.r.t. the domain
Maintain description)
draw itself on a
canvas using
primitive
shapes
We then identify responsibilities and collaborators.
Object-Oriented Thinking | 26

Class Collaborations
Text
Figure Class Collaborations
Circle Canvas
Responsibilities Triangle PDFDocBuilder
Maintain a list of Square Responsibilities
shapes
Save figure as
.pdf file
act as a Canvas
Class Collaborations
Color
Canvas
Class Collaborations

Responsibilities Canvas
SVGDocBuilder
draw text, lines,
bzier curves, Responsibilities

ellipses and points Save figure as


.svg file
act as a Canvas
Evolving the Application
Object-Oriented Thinking | 27

If we follow the new path - i.e, each XDocBuilder is


basically a canvas - we end up with this:

BMPDocBuilder Canvas Bzier curve

JPEGDocBuilder Square

DXFDocBuilder Circle

SVGDocBuilder Triangle

PDFDocBuilder
Background Literature
Object-Oriented Thinking | 28

The International
A Laboratory For Teaching
Conference on Object Object-Oriented Thinking

Oriented Programming, Kent Beck, Apple Computer, Inc.


Ward Cunningham, Wyatt Software Services, Inc.

Systems, Languages and


Applications (OOPSLA); ACM It is difficult to introduce both novice and
experienced procedural programmers to the
reduces to teaching the design of objects. We
focus on design whether we are teaching basic

Press, 1989 anthropomorphic perspective necessary for concepts to novices or the subtleties of a
object-oriented design. We introduce CRC complicated design to experienced object
cards, which characterize objects by class name, programmers.
responsibilities, and collaborators, as a way of
giving learners a direct experience of objects. Rather than try to make object design as much
We have found this approach successful in like procedural design as possible, we have found
teaching novice programmers the concepts of that the most effective way of teaching the
objects, and in introducing experienced idiomatic way of thinking with objects is to
programmers to complicated existing designs. immerse the learner in the object-ness of the
material. To do this we must remove as much
familiar material as possible, expecting that
1. Problem details such as syntax and programming
environment operation will be picked up quickly
The most difficult problem in teaching object- enough once the fundamentals have been
oriented programming is getting the learner to thoroughly understood.
give up the global knowledge of control that is
possible with procedural programs, and rely on It is in this context that we will describe our
the local knowledge of objects to accomplish perspective on object design, its concrete
their tasks. Novice designs are littered with manifestation, CRC (for Class, Responsibility,
regressions to global thinking: gratuitous global and Collaboration) cards, and our experience
variables, unnecessary pointers, and using these cards to teach both the fundamentals
inappropriate reliance on the implementation of and subtleties of thinking with objects.
other objects.

Because learning about objects requires such a 2. Perspective


shift in overall approach, teaching objects
Procedural designs can be characterized at an
Permission to copy without fee all or part of this material is granted provided
Object-Oriented Thinking
The inheritance relationship
A common pitfall in object-oriented design is the
inheritance relation.
Object-Oriented Thinking | 30

Now lets assume we want to further evolve our library and


add support for Rectangles

42
A common pitfall in object-oriented design is the
inheritance relation.
Object-Oriented Thinking | 31

Now lets assume we want to further evolve our library and


add support for Rectangles
Should Rectangle inherit from Square?
Should Square inherit from Rectangle?
Is there some other solution?

42
A common pitfall in object-oriented design is the
inheritance relation.
Object-Oriented Thinking | 32

Now lets assume we want to further evolve our library and


add support for Rectangles
Should Rectangle inherit from Square?
Should Square inherit from Rectangle?
Is there some other solution?
A first test:
Is a Rectangle a Square?
42
A common pitfall in object-oriented design is the
inheritance relation.
Object-Oriented Thinking | 33

Now lets assume we want to further evolve our library and


add support for Rectangles
Should Rectangle inherit from Square?
Should Square inherit from Rectangle?
A first test:
Is there some other solution?
Is a Rectangle a Square?
42

No.
A common pitfall in object-oriented design is the
inheritance relation.
Object-Oriented Thinking | 34

Now lets assume we want to further evolve our library and


add support for Rectangles
Should Rectangle inherit from Square?
Should Square inherit from Rectangle?
Is there some other solution?
A first test:
Is a Square a Rectangle?
42

Well yes, but how


about a Squares behavior?
A common pitfall in object-oriented design is the
inheritance relation.
Object-Oriented Thinking | 35

Now lets assume we want to further evolve our library and


add support for Rectangles
Should Rectangle inherit from Square?
Should Square inherit from Rectangle?
Is there some other solution?
A first test:
Is a Square a Rectangle?
42

Well yes, but how


about a Squares behavior?
Object-Oriented Thinking
Summary
A large number of Design Heuristics and Design
Principles exists that help you to design better
programs.
Object-Oriented Design | 37

Low Coupling
High Cohesion
Single Responsibility Principle
Dont repeat yourself
No cyclic dependencies
Liskov Substitution Principle
Open-Closed Principle
...
Goal of the Lecture | 38

The goal of this lecture is to enable you to


systematically carry out small(er) software
projects that produce well-designed software.

- Identifying and (re-)distributing responsibilities among objects / classes is


one of the major tasks when designing and evolving object-oriented
programs.
- Having classes with well identified responsibilities facilitates the
comprehension, maintenance and evolution of the software.

Vous aimerez peut-être aussi