Lecture 6 Program development
Lecture 6 Program development
Lecture 6:
Program Development
Acknowledgement
1
9/26/24
Outlines
• Models for program design
• Program development
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Program Design
The task of program design is to represent the software
architecture in a form that can be implemented as one or
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
2
9/26/24
UML Models
UML models (diagrams and specifications) can be used for
almost all aspects of program design.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
UML Models
Models used mainly for requirements
• Use case diagram shows a set of use cases and actors, and
their relationships.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
3
9/26/24
Class Diagram
A class is a description of a set of objects that share
the same attributes, methods, relationships, and
semantics.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Window name
Origin attributes [local, instance, and class
size (static) variables]
Open()
Close()
Move() methods
Display()
responsibilities [optional text]
import java.awt.Graphics;
class HelloWorld extends java.applet.Applet {
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
4
9/26/24
class
HelloWorld
name
methods paint()
class
10
5
9/26/24
Notation: Relationships
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
child parent
A generalization is a relationship in which objects of the
specialized element (child) are substitutable for objects of
the generalized element (parent).
HelloWorld
dependency
Graphics
paint()
12
6
9/26/24
Notation: Association
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
0..1 *
employer employee
13
Association
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
ParkingLot
1
1 ... *
ParkingSpace
location
is_available()
14
7
9/26/24
Specification Fields
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
8
9/26/24
17
18
9
9/26/24
19
20
10
9/26/24
21
Candidate Classes
Noun Comments Candidate
Library the name of the system no
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Book yes
Journal yes
Copy yes
ShortTermLoan event no (?)
LibraryMember yes
Week measure no
MemberOfLibrary repeat of LibraryMember no
Item book or journal yes (?)
Time abstract term no
MemberOfStaff yes
System general term no
Rule general term no
22
11
9/26/24
Book is an Item
Journal is an Item
Copy is a copy of a Book
LibraryMember
Item
MemberOfStaff is a LibraryMember
Is an Item needed?
23
Methods
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
24
12
9/26/24
MemberOfStaff LibraryMember
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
1 1
on loan on loan
0..12 0..*
Journal Copy Book
is a copy of
1..* 1
25
26
13
9/26/24
Design is iterative
• As the process moves from preliminary design to
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
27
among them.
• Sequence diagrams: Time ordering of messages
28
14
9/26/24
domain name
service
TCP connection
HTTP get
Client Servers
29
30
15
9/26/24
Notation: Interaction
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
display
31
Actions on Objects
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
32
16
9/26/24
33
34
17
9/26/24
35
Outlines
• Models for program design
• Program development
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
36
18
9/26/24
• incremental compiler
• a debugger
37
19
9/26/24
39
40
20
9/26/24
41
42
21
9/26/24
Class Hierarchies
Since the design of class hierarchies is difficult it is good
practice to use existing frameworks.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Often many of the classes will have been written for you,
or abstract classes are provided that you can use as a basis
for your subclasses.
Examples:
• class hierarchies that are part of programming languages
• toolkits (e.g., for graphical user interfaces)
• design patterns
• frameworks for web development and mobile apps
43
44
22
9/26/24
45
23
9/26/24
47
(Python version)
An advanced development environment combines:
• integrated development environment (IDE)
• application framework
• user interface layout manager and more
Example:
Apple’s Xcode for iOS
48
24
9/26/24
50
25
9/26/24
51
26
9/26/24
Production Programming
Murphy's Law:
If anything can go wrong, it will.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Challenges:
• Code has to be maintained over the long term, with
different system software.
• Interfaces will be used in new and unexpected ways.
• Every possible error will eventually occur at the worst
possible time (bad data, failures of hardware and
system software).
• There are likely to be security attacks.
53
Production Programming
Robust programming
• Write simple code.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
54
27
9/26/24
Outlines
• Models for program design
• Program development
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
55
Group activities
• What are the differences between the bridge pattern
and the facade pattern? Give an example for each
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
pattern.
• Explore case studies of successful and unsuccessful
software designs. Discuss lessons learned and best
practices.
56
28
9/26/24
Software Reuse
It is often good to design a program to reuse existing
components. This can lead to better software at lower
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
cost.
Potential benefits of reuse
• Reduced development time and cost
• Improved reliability of mature components
• Shared maintenance cost
Potential disadvantages of reuse
• Difficulty in finding appropriate components
• Components may be a poor fit for application
• Quality control and security may be unknown
57
Evaluating Software
Software from well established developers is likely to be
well written and tested, but still will have bugs and
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
58
29
9/26/24
Evaluating Software
Maintenance
When evaluating software, both commercial and open
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
59
30
9/26/24
61
62
31
9/26/24
63
New implementation
The original implementation may be problematic,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
64
32
9/26/24
65
66
33
9/26/24
Design Patterns
Design patterns are template designs that can be used in
various systems. They are particularly appropriate in
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
67
Design Patterns
Sources:
1. E. Gamma, R. Helm, R. Johnson, and J. Vlissides,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
68
34
9/26/24
Structural
These concern class and object composition. They use
inheritance to compose interfaces and define ways to
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
69
Structural
• Decorator dynamically adds/overrides behaviour in an
existing method of an object.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
70
35
9/26/24
71
Delegation
A class is said to delegate to another class if it
implements an operation by resending a message to
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
another class.
Delegation is an alternative to inheritance that can be
used when reuse is anticipated.
72
36
9/26/24
Notation
dependency
delegation
inheritance
73
Problem description:
Convert the interface of a legacy class into a different
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
37
9/26/24
NewClient OldClient
dependency
NewClass LegacyClass
request() existingRequest()
75
italic
ClientInterface LegacyClass
request() existingRequest()
Adapter delegation
inheritance request()
76
38
9/26/24
77
78
39
9/26/24
Client
alternative
implementations
ConcreteImplementorA
ConcreteImplementorB
79
Client
Implementor
ConcreteImplementorA
ConcreteImplementorB
80
40
9/26/24
Client
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Abstraction Implementor
(described later)
81
Solution:
• The Abstraction class defines the interface visible to
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
the client.
• Implementor is an abstract class that defines the
lower-level methods available to Abstraction.
• An Abstraction instance maintains a reference to
its corresponding Implementor instance.
• Abstraction and Implementor can be refined
independently.
82
41
9/26/24
Bridge: Consequences
• Client is shielded from abstract and concrete
implementations
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
83
84
42
9/26/24
85
86
43
9/26/24
to select network
NetworkInterface
NetworkConnection
open()
Open() close()
Close() send()
Send()
setNetworkInterface()
Ethernet WirelessNet
open() open()
close() close()
send() send()
87
88
44
9/26/24
ConcreteStrategy
Context
ContextInterface() Strategy
algorithmInterface()
89
Strategy: Consequences
Consequences
ConcreteStrategies can be substituted transparently from
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Context.
Policy decides which Strategy is best, given the
current circumstances.
New policy algorithms can be added without modifying
Context or Client.
90
45
9/26/24
Facade
Facade
service2()
92
46
9/26/24
Facade: Consequences
Consequences:
• Shields a client from the low-level classes of a
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
subsystem.
• Simplifies the use of a subsystem by providing higher-
level methods.
• Enables lower-level classes to be restructured without
changes to clients.
Note. The repeated use of Facade patterns yields a
layered system
93
94
47
9/26/24
Client
Component
Leaf Composite
95
Solution
• The Component interface specifies the services that
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
96
48
9/26/24
Composite: Consequences
Consequences:
• The client uses the same code for dealing with Leaves
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
or Composites.
• Leaf-specific behavior can be changed without
changing the hierarchy.
• New classes of Leaves can be added without changing
the hierarchy.
97
98
49
9/26/24
Solution:
A platform (e.g., the application for a specific
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
99
Client AbstractFactory
createProductA
createProductB
AbstractProductB
AbstractProductA
100
50
9/26/24
Client AbstractFactory
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
createProductA
ConcreteFactory1
AbstractProductA createProductA
101
Client AbstractFactory
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
createProductA
createProductB
AbstractProductB ConcreteFactory1
AbstractProductA
createProductA
createProductB
ProductB
There could be several
ProductA ConcreteFactory classes,
each a subclass of
AbstractFactory
102
51
9/26/24
103
Discussion
See the interesting discussion in Wikipedia (December
16, 2016):
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
104
52
9/26/24
An Exam Question
A company that makes sports equipment decides to
create a system for selling sports equipment online.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
An Exam Question
Careful design during Phase 1 will help the subsequent
development of new components in Phase 2.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
(a) For the interface between the ordering system and the
customer database:
i). Select a design pattern that will allow a gradual transition
from Phase 1 to Phase 2.
Bridge design pattern
ii). Draw a UML class diagram that shows how this design
pattern will be used in Phase 1.
If your diagram relies on abstract classes, inheritance,
delegation or similar properties be sure that this is clear
on your diagram.
106
53
9/26/24
An Exam Question
Client
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Ordering System
OrderingAbstraction DBImplementor
ConcreteDBImplementorA
ConcreteDBImplementorB
107
An Exam Question
(b)How does this design pattern support:
i). Enhancements to the ordering system in Phase 2?
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
By subclassing OrderingAbstraction
ii). A possible replacement of the customer database in
Phase 2?
By allowing several ConcreteDBImplementor classes.
108
54
9/26/24
Legacy Systems
Many data intensive systems, e.g., those used by banks,
universities, etc. are legacy systems. They may have been
developed forty years ago as batch processing, master file
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
109
Legacy Systems
The Worst Case
A large, complex system that was developed several decades ago:
• Widely used either within a big organization or by an unknown
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
number of customers.
• All the developers have retired or left.
• No list of requirements. It is uncertain what functionality the
system provides and who uses which functions.
• System and program documentation are incomplete and not kept up
to date.
• Written in out-of-date versions of programming languages using
system software that is also out of date.
• Numerous patches over the years have ignored the original system
architecture and program design.
• Extensive code duplication and redundancy.
• The source code libraries and production binaries may be
incompatible.
110
55
9/26/24
Legacy Requirements
Planning
In conjunction with the client develop a plan for
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
111
Legacy Requirements
Requirements as implied by the system design
• If there is any system documentation, what does it
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
112
56
9/26/24
Legacy Code
Source code management
• Use a source code management system to establish a
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
113
Legacy Code
Rebuilding the software
An incremental software development process is often
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
57
9/26/24
Legacy Code
• Move to current versions of programming languages and
systems software.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
115
116
58