Vous êtes sur la page 1sur 21

CS2110: SW Development

Methods
Software Design

Design of methods (functions)


Class design
CRC cards
UML class and sequence diagrams

Old problems
Before OO, many languages followed the procedural
paradigm
A program was based on functions that worked like static
methods in Java
A main function started things

functional decomposition
Break problem down
Function deals with subproblem
Hierarchical. Levels of abstraction

Problems:

Control and coordination centered in main method


Required changes have big impacts
Logic may be distributed
Data changes affect may modules

Functions:
What we call them:
functions, methods, modules
Functions are a core construct in OO and
non-OO programs
Principals of good functions apply in either
language
But less of a problem in OO can you think
why?

What makes a function good?


Your answers:

Good qualities in functions:


Lots written about this!
Clean Code: A Handbook of Agile SW
Craftsmanship
Robert C. Martin
Read Chapter 3 in UVa digital library version

Good Qualities in Functions


Small
How small?

Do One Thing: Strong Cohesion


Why?
One level of abstraction per function
Thus, functions fall into levels of abstraction

Name reflects the one task it does

Interface Qualities of Functions


What about its interface?
Inputs, return value/arguments
Other sources of data or output
Side effects

Some rules?

Good Qualities in Functions (2)


Number of arguments: small
Flag arguments
What are they? What bad thing do they
suggest is happening?

Avoid side effects


Avoid output arguments
Arguments that are changed after the function
executes
Java: possible for mutable classes

Good Qualities in Functions (3)


Dont Repeat Yourself (DRY)
Command/Query Separation
Do something. Or answer something.
Dont do both.

Returning Errors
Return value? Output argument?
Burden on caller?

Exceptions: what advantages?

Classes
So far weve just talked about objects
We note that many objects are a
type of the same kind of thing
The same abstraction
E.g. 1, 2, 7 and 10 whole numbers
E.g. 1, 3.5, 25, pi numbers
Bob, Sally, Joe Students
But, they are Persons too, arent they?

cs201, cs202, engr162 Courses

Classes, Type
Classes define a set of objects with the same
properties (state, behavior)
A class definition serves as a cookie cutter for
creating new objects
Instantiation of an object of a certain class
In Java, we do this with new and a constructor is
called
Creates an individual object, also called an instance

Variables and data objects have a type


What the rules are for that object
An objects class is one form of object-type
Also any superclass it extends, and any interface it
implements

Finding Classes from


Specifications
Study the textbook, Sect. 2.3.1
Next In-Lab exercises
Several problems for groups to do in lab
Later youll get sample solutions for all
these problems

Sample Problem: Library


Loans

Problem overview: Imagine you were going to write a set of


classes that would be part of a software system to support loans in
a library. A description might be:

The library contains books and journals. It may have


several copies of any given book. Books and journals have
titles and a call-number. Each copy has a copy-number.
For each book we record the author. Some of the books are
on "reserve" and can only be checked-out for 3 hours, but
all other books can be borrowed as a regular loan (for a
three week period). Members of the library can normally
borrow up to six items at a time, but staff members can
borrow up to ten items. Members of staff can borrow
journals, but not regular members. The system must keep
track of when books and journals are borrowed and
returned. Our system might want find out what items have
been borrowed by a given member, and which copies of an
item have been checked out and by whom.

Class Identification
What are possible classes here?
Why?
<your answers here>
How do you recognize classes?
What makes a good class name?

Sample Problem: Library


Loans
Problem overview: Imagine you were going to write a set
of classes that would be part of a software system to support
loans in a library. A description might be:
The library contains books and journals. It may have
several copies of any given book. Books and journals have
titles and a call-number. Each copy has a copy-number.
For each book we record the author. Some of the books are
on "reserve" and can only be checked-out for 3 hours, but all
other books can be borrowed as a regular loan (for a three
week period). Members of the library can normally borrow up
to six items at a time, but staff members can borrow up to
ten items. Members of staff can borrow journals, but not
regular members. The system must keep track of when
books and journals are borrowed and returned. Our system
might want find out what items have been borrowed by a
given member, and which copies of an item have been
checked out and by whom.

Methods and Fields


Pick a class: name its methods and
fields

Class Relationships
Do any classes have structural links
to other classes?
link might mean part-of relationship.
This is a static relationship
Are such links one-to-one, one-to-many?

Which objects might call methods on


objects of another class?
This is a dynamic relationship (while the
program is running)

Another View on Defining


Classes
Classes have
Responsibilities
In words: what is its role? Data
encapsulation and management? Organizer
of other objects? Some level of control or
task-execution?
Methods implement these

Relationships
Static connections to other objects
Dynamic interactions with other objects

The CRC Card Technique


Read info on CRC cards here:
http://www.agilemodeling.com/artifa
cts/crcModel.htm

Vous aimerez peut-être aussi