Introduction to Software Design

advertisement
The Software Challenge
• People may come and go, but software may remain
• A software product is often expected to be used for an
extended period of time by someone who did not
write the program and who is not intimately familiar
with its internal design
• Software may evolve
• New features may be added, environments may
change, so initial specification may be incomplete
1
The Software Specification Challenge
• Software specification is not easy
• It should be generated at the beginning of project and
maintained up-to-date while the software goes
through changes
• It should be clarified through extensive interaction
between the users and the system analyst, and then
approved by the users
• It should be clear and understandable to any
programmer
2
The Software Life Cycle – The Life and
Death of Software
• Software products go through several stages as they
mature from initial concept to finished product
• The sequence of stages is called a life cycle
3
Software Life Cycle Models
• Waterfall model: simplest way of organizing activities
that transform software from one stage to another
• Activities are performed in sequence and the results of
one flows into the next
4
Waterfall Model
1
2
3
4
5
Requirements (requirements are determined)
Analysis (requirements are analyzed)
Design (components are designed)
Implementation (components are implemented)
Testing (components are assembled and tested as a
whole)
5
Software Life Cycle Models
• Waterfall model is simple but unworkable
• Fundamental flaw is assumption that each stage can
and must be completed before the next one occurs
• Sometimes, it is not until the product is finished that the
user can fully express his or her requirements
6
Software Life Cycle Activities
1) Requirements
Specification
2) Design
a. Architectural Design
b. Component Design
c. Detailed Design
4) Test
a. Unit Test
b. Integration Test
c. Acceptance Test
5) Installation
6) Maintenance
3) Implementation
7
Design Principles in Software Life Cycle
Activities
• Top-down approach: breaking a system into a set of
smaller subsystems
• Object-oriented approach: identification of a set of
objects and specification of their interactions
• UML diagrams are a design tool to illustrate the
interactions between
• Classes
• Classes and external entities
8
Requirements Analysis, Use Cases, and
Sequence Diagrams
• First step in analysis is to study the problem of input and
output requirements carefully to make sure they are
understood and make sense
• Use case: list of the user actions and system responses
for a particular sub-problem in the order that they are
likely to occur
• Sequence diagram: shows all the objects involved in
this use case across the horizontal axis, time is shown
along the vertical axis
9
Pre- and Postconditions
• Precondition: a statement of any assumptions or
constraints on the method data before the method
begins execution
• Postcondition: a statement that describes the result of
executing a method
10
Using Abstraction to Manage Complexity
• Abstraction is a model of a physical entity or activity
• Abstraction helps programmers deal with complex issues
in a piecemeal fashion
• Procedural abstraction: distinguish what is to be
achieved by a procedure from its implementation
11
Using Abstraction to Manage Complexity
(cont’d)
• Data abstraction: specify the data objects for a
problem and the operations to be performed on them
without concern for their representation in memory
• Representation of a data object is irrelevant to other
classes that access to it only via its methods 
Information hiding: concealing the details of a class
implementation from users of the class
12
An Example: Telephone Directory
• Maintain a collection of telephone directory entries,
where each entry is referred to by a unique name.
• Can read from a file, save to a file, lookup, add, remove,
and change phone number
13
Dependencies Among Possible Actions
14
Object Relations
15
Things you already know (about) ...
•
•
•
•
•
Java programs (you know and love)
Classes and objects (you can create and use)
Inheritance (you understand and can extend)
Abstract classes (you remember what they are)
Interfaces (your contractual obligations)
• Interfaces are a key idea in CSC220
16
Interfaces
• The interface specifies the names, parameters, and
return values of the ADT methods without specifying how
the methods perform their operations and without
specifying how the data is internally represented
• Each class that implements an interface must provide
the definitions of all methods declared in the interface
17
Interfaces
• You cannot instantiate an interface; that is, can’t invoke
“new INTERFACE_NAME”
• You can declare a variable that has an interface type
and use it to reference an actual object
• A Java interface is a contract between the interface
designer and the programmer who codes a class that
implements the interface
18
Public Methods of PhoneDirectory Interface
Method
Action
public void loadData(String fileName)
Loads the data from the data file whose name
is given by fileName
public String addOrChangeEntry(String name,
String number)
Changes the number of the individual with the
name to the number
public String lookupEntry(String name)
Searches the directory for the name
public String removeEntry(String name)
Removes the entry with the given name
public void saveData(String fileName)
Saves the data in a load-able format in the
data file whose name is given by fileName
19
The PDUserInterface Interface
• There is only one required public method,
processCommands, which takes input from the user and
executes the command
• Two different implementations:
• PDGUI: a GUI-based implementation
• PDConsoleUI: a console-based implementation
20
PDGUI: Implementation as GUI
• Uses JOptionPane dialog windows
String[] commands = {"Add/Change Entry", "Look Up Entry”, "Remove Entry", "Save Directory", "Exit”};
do {
choice = JOptionPane.showOptionDialog(
null,
“Select a Command”,
“PhoneDirectory”,
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTIONE_MESSAGE,
null,
command,
command[commands.length – 1]);
switch (choice) {
case 0: doAddChangeEntry(); break;
case 1: doLookupEntry(); break;
case 2: doRemoveEntry(); break;
case 3:
case 4: doSave(): break;
default: // Do nothing.
}
} while (choice != commands.length – 1);
// No parent
// Prompt message
// window title
// Option type
// Message type
// Accompanying icon
// Choice names (array)
// Default command
21
PDConsoleUI: Implementation Using a
Console
• Uses System.out to display the menu of choices and
results.
• It also uses a Scanner object (scIn) created out of
System.in to read data from the keyboard.
// Constructor
/** Default constructor. */
public PDConsoleUI() {
scIn = new Scanner(System.in);
}
…
choice = scIn.nextInt(); // Get next choice.
scIn.nextLine();
// Skip trailing newline.
22
ArrayBasedPD that Implements
PhoneDirectory Interface, Private Data Fields
Data Field
Attribute
private static final int INITIAL_CAPACITY
The initial capacity of the array (or the array
size). It has the “final” attribute so change is
not allowed.
private int capacity
The current capacity (or the array size), so
the capacity changes
private int size
The number of entries held in the directory
private DirectoryEntry[] theDirectory
The directory realized as an array of
DirectoryEntry objects
private String fileName
The name of the data file
private boolean modified
A boolean variable that maintains whether
any change has been made to any entry
since the last time the data were saved or
loaded. Data is automatically saved at
closing if this variable is true.
23
The Private Methods of the
ArrayBasedPD Class
Private Method
Action
private int find(String name)
Searches the array for the name and
returns the position of the name; -1
indicates that the name was not found
private void add(String name, String
number)
Adds to the array a new entry with the given
name and number
private void reallocate()
Creates a new array with twice the capacity
of the current one with the same entries
24
DirectoryEntry Class
Data Field
Attribute
private String name
The name of an individual
private String number
The phone number of the individual
Constructor
Action
public DirectoryEntry(String name, String
number)
Create an entry with the name and the
number
Method
Action
public String getName()
Retrieves the name of an individual
public String getNumber()
Retrieves the number of an individual
public void setNumber(String number)
Sets the number of an individual to the give
number
25
Implementing and Testing the Array-Based
Phone Directory
• The main loop of ReadData has two notable points
• The while and if statements combine an assignment with a
conditional statement
• The break statement allows exiting of the while loop without
storing an entry
// Read each name and number and add the entry to the array.
BufferedReader in = new BufferedReader(new FileReader(new File (this.filename)))
while ((name = in.readLine()) != null) { // Read name and number from successive lines.
if ((number = in.readLine()) != null) { break; }
// No number read, exit loop.
// Add an entry for this name and number.
this.add(name, number);
}
26
Code Reuse
• A part of program can be reused for other programs
• If there is a code for maintaining a phone book (adding,
removing, editing, loading, and storing), some of the
ideas and concepts used for the code can be used for
writing a code for maintaining business cards.
27
Maximizing Code Reuse
• View the program to be developed as a process of
dealing with data
• The data has to be maintained during execution of the
program
• The data may be read from and/or stored into files
• The data may be generated from input
• Various operations on the data may be performed
28
Abstract Data Types
• Abstract data type (ADT) = the combination of data
together with its methods (how the data objects are
accessed)
• ADTs specify a set of required methods, but do not
specify how those methods should be implemented
(that’s why they are called “abstract”)
• “Data structures” quite often refer to ADTs
• For frequently used ADTs the most efficient universal
(applicable to the vast majority of programming
languages) implementations are known
29
Download