Introduction to Software Design - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Introduction to Software Design

Description:

Therefore, it is important to design and document software in an organized way ... that helps us develop a complicated and large program in a piecemeal fashion. ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 45
Provided by: phil197
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Software Design


1
Introduction to Software Design
  • Chapter 1

2
Chapter Objectives
  • To understand what activities take place in each
    phase of the software life cycle
  • To learn how to draw UML class diagrams to
    document the relationships between classes.
  • To learn to use data abstraction, procedural
    abstraction, and information hiding to manage
    complexity
  • To learn the role of abstract data types in
    building models of computer systems and how to
    implement them using classes and interfaces
  • To understand the software design process by
    following the design and implementation of an
    array-based telephone directory

3
Software Life Cycle Models
  • Waterfall model simplest way of organizing
    activities that transforms software from one
    stage to another

4
  • Problem 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
  • New alternatives to waterfall model (e.g. Unified
    Model)
  • To develop a software product in iterative and
    incremental ways
  • Beyond scope of CS46B

5
UML Class Diagrams
  • As a design tool, a UML Class Diagram shows the
    classes and their relationships
  • is-a relationship
  • One class is a subclass of the other class
    (inheritance)
  • One class implements an interface
    (implementation)
  • has-a relationship a class declares instance
    fields of other class types
  • class Car extends Vehicles
  • private Tire tires

6
UML Relationship Symbols(Chapter 17, Java
Concepts, 4th Edition,by Cay Horstmann)
  • Inheritance is-a relationship
  • Interface Implementation a class implements an
    interface is-a relationship
  • Aggregation has-a relationship, a special form
    of association.
  • Dependency uses relationship a class depends on
    another if one of its method uses an object of
    the other.

7
1.2 Abstraction helps Manage Complexity
  • An abstraction is a logical view of a physical
    entity or activity
  • Abstraction is a powerful technique that helps us
    develop a complicated and large program in a
    piecemeal fashion.
  • Procedural abstraction
  • Data abstraction
  • Information Hiding

8
Procedural Abstraction
  • The philosophy that procedure development should
    separate the concern of what a procedure (Java
    method) to do from the details of how it is to be
    implemented.
  • Example a user program of findMax method
  • public class Test
  • public static void main (String args)
  • System.out.println(findMax(1,2,3))
  • The user needs to know what findMax does, but
    does not have to know how it is implemented.
  • /
  • _at_param three integers a, b, and c
  • _at_return maximum of the parameters a, b and c
  • /
  • public static int findMax (int a, int b, int c)


9
Procedural Abstraction (continued)
  • The designer concerns about how the method is
    implemented. There can be different ways to
    implement a method to achieve the same task.
  • Alternative A
  • public static int findMax (int a, int b, int c)
  • int max a
  • if (b gt max) max b
  • if (c gt max) max c
  • return max
  • Alternative B
  • public static int findMax (int a, int b, int c)
  • return Math.max(Math.max(a, b), c)
  • And more

10
Procedural Abstraction (continued)
  • Why procedural abstraction ?
  • In a design stage, you can define what a method
    does, and use it before you know how to implement
    it.
  • The implementation can be improved to adopt more
    efficient way to achieve the same task without
    affecting the user program of the method.

11
Data Abstraction
  • To specify the data objects for a problem and the
    operations to be performed on these data objects
    without concerning the details of their
    implementation.
  • Through data abstraction, we can have a logical
    view of data objects, as opposed to a detailed
    implementation
  • Example Logical view of a Bag object

Bag myBag new MyBag() myBag.add(10) myBag.add(
20)
add
10
20
12
Data Abstraction (continues)
  • public class Bag
  • private int data
  • private int dataSize
  • public Bag()
  • data new int100
  • public void add(int x)
  • datadataSize x
  • dataSize

Detailed Implementation of a Bag object
13
Information Hiding
  • The process of hiding the details of a class
    implementation from users of the class.
  • The user does not get direct access to the part
    of the object or their implementations they can
    be accessed only though by public methods
    supplied with the object.
  • Information hiding principle relies on two access
    modifiers private and public
  • Example

public class Entry private String name
private String number public void
setName(String n) name n
Entry e new Entry() e.name Smith
//no e.setName(Smith)// yes
14
Java Interfaces
  • Knowledge of Java interface is a fundamental to
    learn the rest of chapter.
  • Refer to the supplementary handout about Java
    interfaces. If needed, you may also want to
    review Chapter 11 of Java Concepts, 4th Edition.

15
1.3 Abstract Data Types, Interfaces, and Pre- and
Postconditions
  • Abstract data type (ADT) a set of data values
    and associated operations independent of any
    particular implementation.
  • A Java interface
  • specifies the names, parameters, and return
    values of the methods of an ADT
  • does not specify how the methods should be
    implemented and how the data are internally
    represented
  • A Java class is an implementation of an ADT

16
Java Interface Example
  • / The interface for the phone directory user
  • interface.
  • /
  • public interface PDUserInterface
  • / Abstract method that processes user's
    commands.
  • _at_param thePhoneDirectory PhoneDirectory
    object
  • that contains the data to be
    displayed
  • and/or changed
  • /
  • void processCommands(PhoneDirectory
    thePhoneDirectory)
  • The interface definition shows the method
    signature only
  • without its body.
  • The key words public abstract are implicit in
    the method
  • heading.

17
  • A Java class is an ADT implementation.
  • Each Java class that implements an interface must
    provides the implementation of all methods
    declared in the interface, as well as data fields
    of the ADT
  • There can be more than one class that implements
    the interface. For example,
  • public class PDConsoleUI implements
    PDUserInterface
  • public class PDGUI implements PDUserInterface

18
Contract and Interfaces
  • A Java interface (e.g., PDUserInterface) is a
    promise that a class that implements the
    interface (e.g., PDGUI or PDConsoleUI) must
    fulfill.
  • Any program (e.g., PDApplication presented in
    the next page) that uses a class implementing
    interface knows exactly what methods are
    available (e.g., processCommands) in that class.
  • Either implementation (e.g. PDGUI or PDConsoleUI)
    can be used without affecting other classes
    (e.g., PDApplication) that interact with it,
    because both of them fulfill the promise.

19
  • Example part of PDApplication.java of textbook
    pp.48
  • public class PDApplication
  • pubic static void main (String args)
  • PhoneDirectory phoneDirectory new
    ArrayBasedPD()
  • phoneDirectory.loadData(args0)
  • phoneDirectory.addChange()
  • phoneDirecotry.save()
  • PDUserInterface phoneDirectoryInterface
    new PDConsoleUI()
  • phoneDirectoryInterface.processCommands(phon
    eDirectory)

If you want to use PDConsoleUI() instead of
PDGUI(), then new PDGUI() ? new PDConsoleUI()
will do. The rest of the program will not be
affected.
20
Preconditions and Postconditions
  • Precondition a statement of any assumptions or
    constraints on the method parameters before the
    method begins execution
  • Postcondition a statement that describes the
    result of executing a method.
  • The return value is computed correctly (accessor)
  • The object is in a certain state after the method
    call is completed (mutator)
  • A methods preconditions and postconditions serve
    as a contract between a method caller and the
    method implementer, and are documented in the
    javadoc comment of the method.

21
Preconditions and Postconditions (continues)
  • / Deposits money into this account.
  • _at_param amount the amount of money to
    deposit
  • precondition amount is positive
  • postcondition adds the specified amount to
    balance
  • /
  • public void deposit(double amount)
  • balance balance amount
  • As a general rule, you should write a
    postcondition for all void methods.

22
Preconditions and Postconditions (continues)
  • /
  • Returns the current balance of this account.
  • _at_return the account balance
  • /
  • public double getBalance()
  • return balance
  • If _at_return statement describes the postcondition
    of method, you should not repeat it in a
    postcondition statement.

23
Homework (no submission is required)
  • Download the source codes of this case study from
    the publisher site.
  • Write a PDApplication class of your own since the
    publisher site doesnt supply it. Refer to pp.48
    of the text.
  • Compile and run the PDApplication.

24
1.5 Case Study Design of Phone
DirectoryIdentify all classes and interfaces
that will be the part of the problem solution
  • Interfaces
  • PDUserInterface
  • PhoneDirectory
  • Classes
  • PDApplication with a main method
  • PDGUI implements PDUserInterace
  • ArrayBasedPD implements PhoneDirectory
  • DirectoryEntry

25
Design identify data fields of these classes and
provide algorithms for their methods if needed.
DirectoryEntry class
26
ArrayBasedPD class
27
ArrayBasedPD class (continued)
28
ArrayBasedPD class (continued)
Next, describe the algorithms of the methods
(text pp. 32-33). One example is presented in the
next slide.
29
Algorithm for Method loadData
  • Create a Scanner for the input file
  • Read the first name
  • While the name is not null
  • Read the number
  • Add a new entry using method add (a private
    method)
  • Read the next name.

30
PDGUI
processCommands()
31
1.6 Implementing (Coding)the Array-Based Phone
Directory
  • We will study the implementation of the
    ArrayBasedPD class. (You need to know how to use
    a partially filled array in Java (CS46A subject))
  • ArrayBasedPD is a concrete class that implements
    the PhoneDirectory interface.
  • What if you want to develop the PhoneDirectory
    based on ArrayList ? or LinkedList ?
  • What specific actions should you take ?
  • We will go over the source code of
    ArrayBasedPD.java in class.

32
Partially Filled Arrays (PFA)(Java Concepts, 4th
edition)
33
Removing an element from a PFA(If the order of
elements doesnt matter)
  • char data new char7

data
data
data.length 7
data.length 7
A
B
C
D
E
A
B
E
D
E
0 1 2 3 4 5 6
0 1 2 3 4 5 6
dataSize
dataSize
5
4
34
Removing an element from a PFA(If the order of
elements doesnt matter)
  • Suppose the element to be removed is datapos
    and there are dataSize number of elements in
    data.
  • If the order of the elements doesnt matter,
  • use the following algorithm.
  • Overwrite the removed element with the last
    element
  • datapos datadataSize-1
  • dataSize --

35
Removing an element from a PFA(If the order of
elements matters)
  • char data new char7

data
data
data.length 7
data.length 7
1st 2nd
A
B
C
D
E
A
B
D
E
E
0 1 2 3 4 5 6
0 1 2 3 4 5 6
dataSize
dataSize
5
4
36
Removing an element from a PFA(If the order of
elements matters)
  • Suppose the element to be removed is datapos
    and there are dataSize number of elements in
    data.
  • If the order of the elements matters,
  • use the following algorithm.
  • Each element after pos should be moved leftward
    one position.
  • for(int i pos i lt dataSize 1 i)
  • datai datai 1
  • dataSize--

37
Insert an element to a PFA
3rd 2nd 1st
data
dataSize
5
A
B
C
D
E
0 1 2 3 4 5 6
dataSize
data
5
A
B
C
D
E
0 1 2 3 4 5 6
dataSize
data
6
A
B
C
D
E
X
0 1 2 3 4 5 6
38
Insert an element to a PFA
  • To insert a new element in datapos, you start
    at the end of the array, move that element
    rightward one position, then go to the one before
    that, until you get to the insertion position.
  • for (int i dataSize i gt pos i--)
  • datai datai 1
  • datapos a new element
  • dataSize

39
Growing an array
40
Growing an array
  • if (dataSize gt data.length)
  • // make a new array of twice the size
  • double newData new double2
    data.length
  • // copy over all elements from data to newData
  • System.arraycopy(data,0,newData,0,dataSize)
  • // abandon the old array and store in data
  • // a reference to the new array
  • data newData

41
Array of objects
  • Array of objects is actually an array of
    references

private DirectoryEntry theDirectory
new DirectoryEntrycapacity
42
1.7 Two Classes that implement PDUserInterface
  • The PDUserInterface defines the processCommands
    method.
  • The case study shows two different classes that
    implement the PDUserInterface
  • PDGUI Class
  • PDConsoleUI
  • Both of the concrete classes should complete the
    processCommand method of PDUserInterface
  • The processCommand methods of these two classes
    will have the same signature but different bodies.

43
public class PDGUI implements PDUserInterface
private PhoneDirectory theDirectory public
void processCommands(PhoneDirectory
thePhoneDirectory) String commands
"Add/Change Entry", "Look Up Entry",
"Remove Entry", "Save
Directory", "Exit" theDirectory
thePhoneDirectory int choice do
choice JOptionPane.showOptionDialog(null,
"Select a Command",
"PhoneDirectory",JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MES
SAGE, null, commands,
commandscommands.length - 1)
switch (choice) case 0
doAddChangeEntry() break case 1
doLookupEntry() break case 2
doRemoveEntry() break case 3
case 4 doSave() break
while (choice ! commands.length -
1) System.exit(0)
44
public class PDConsoleUI implements
PDUserInterface private PhoneDirectory
theDirectory private Scanner scIn
public PDConsoleUI() scIn new
Scanner(System.in) public void
processCommands(PhoneDirectory thePhoneDirectory)
String commands "Add/Change Entry",
"Look Up Entry", "Remove Entry",
"Save Directory",
"Exit" theDirectory thePhoneDirectory
int choice do for (int i
0 i lt commands.length i)
System.out.println("Select " i " "
commandsi) choice scIn.nextInt()
// Read the next choice.
scIn.nextLine() // Skip trailing newline.
switch (choice) case 0
doAddChangeEntry() break case 1
doLookupEntry() break case 2
doRemoveEntry() break case 3
case 4 doSave() break
while (choice ! commands.length - 1)
System.exit(0)
Write a Comment
User Comments (0)
About PowerShow.com