design abstraction
TRANSCRIPT
-
8/12/2019 Design Abstraction
1/29
1
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
-
8/12/2019 Design Abstraction
2/29
2
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
-
8/12/2019 Design Abstraction
3/29
3
The Software Life CycleThe 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
-
8/12/2019 Design Abstraction
4/29
4
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
-
8/12/2019 Design Abstraction
5/29
Waterfall Model
1 Requirements (requirements are determined)
2 Analysis (requirements are analyzed)
3 Design (components are designed)
4 Implementation (components are implemented)5 Testing (components are assembled and tested as a
whole)
5
-
8/12/2019 Design Abstraction
6/29
6
Software Life Cycle Models
Waterfall model is s imp le but u nworkable
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 theuser can fully express his or her requirements
-
8/12/2019 Design Abstraction
7/29
Software Life Cycle Activities
1) Requirements
Specification
2) Design
a. Architectural Design
b. Component Design
c. Detailed Design
3) Implementation
4) Test
a. Unit Test
b. Integration Test
c. Acceptance Test
5) Installation
6) Maintenance
7
-
8/12/2019 Design Abstraction
8/29
8
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/12/2019 Design Abstraction
9/29
9
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
-
8/12/2019 Design Abstraction
10/29
10
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
-
8/12/2019 Design Abstraction
11/29
11
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 beachieved by a procedure from its implementation
-
8/12/2019 Design Abstraction
12/29
12
Using Abstraction to Manage Complexity
(contd)
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
-
8/12/2019 Design Abstraction
13/29
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
-
8/12/2019 Design Abstraction
14/29
Dependencies Among Possible Actions
14
-
8/12/2019 Design Abstraction
15/29
Object Relations
15
-
8/12/2019 Design Abstraction
16/29
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
-
8/12/2019 Design Abstraction
17/29
17
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
-
8/12/2019 Design Abstraction
18/29
18
Interfaces
You cannot instant iate an interface; that is, cantinvoke
newINTERFACE_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
-
8/12/2019 Design Abstraction
19/29
Public Methods of PhoneDirectory Interface
19
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
-
8/12/2019 Design Abstraction
20/29
20
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
-
8/12/2019 Design Abstraction
21/29
21
PDGUI: Implementation as GUI
Uses JOptionPanedialog windowsString[] commands = {"Add/Change Entry", "Look Up Entry, "Remove Entry", "Save Directory", "Exit};
do {
choice = JOptionPane.showOptionDialog(
null, // No parent
Select a Command, // Prompt message
PhoneDirectory, // window title
JOptionPane.YES_NO_CANCEL_OPTION, // Option typeJOptionPane.QUESTIONE_MESSAGE, // Message type
null, // Accompanying icon
command, // Choice names (array)
command[commands.length1]); // Default command
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.length1);
-
8/12/2019 Design Abstraction
22/29
22
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.
-
8/12/2019 Design Abstraction
23/29
ArrayBasedPD that Implements
PhoneDirectory Interface, Private Data Fields
23
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 changesprivate 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 whetherany 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.
-
8/12/2019 Design Abstraction
24/29
The Private Methods of the
ArrayBasedPD Class
24
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
-
8/12/2019 Design Abstraction
25/29
DirectoryEntry Class
25
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
-
8/12/2019 Design Abstraction
26/29
26
Implementing and Testing the Array-Based
Phone Directory
The main loop of ReadData has two notable points
The whileand if statements combine an assignment with a
conditional statement
The breakstatement 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);
}
-
8/12/2019 Design Abstraction
27/29
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
-
8/12/2019 Design Abstraction
28/29
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
-
8/12/2019 Design Abstraction
29/29
29
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(thatswhy they are called abstract)
Datastructuresquite often refer to ADTs
For frequently used ADTs the most efficient universal
(applicable to the vast majority of programming
languages) implementations are known