0% found this document useful (0 votes)
8 views

Lecture 6 Program development

The lecture covers program development, focusing on program design, UML models, and the use of integrated development environments. It emphasizes the importance of proper program design to facilitate implementation and discusses various UML diagrams for system architecture and program design. Additionally, it highlights the iterative nature of design and the role of integrated development environments in software development.

Uploaded by

nguyenducdo35
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 6 Program development

The lecture covers program development, focusing on program design, UML models, and the use of integrated development environments. It emphasizes the importance of proper program design to facilitate implementation and discusses various UML diagrams for system architecture and program design. Additionally, it highlights the iterative nature of design and the role of integrated development environments in software development.

Uploaded by

nguyenducdo35
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

9/26/24

International University, VNU-HCMC

School of Computer Science and Engineering

Lecture 6:
Program Development

Instructor: Nguyen Thi Thuy Loan


[email protected], [email protected]
https://nttloan.wordpress.com/

International University, VNU-HCMC

Acknowledgement

• The following slides are referenced from Cornell


University-Computing and Information Science.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

1
9/26/24

International University, VNU-HCMC

Outlines
• Models for program design
• Program development
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Reuse and design patterns

International University, VNU-HCMC

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

more executable programs.


Given a system architecture, the program design specifies:
• programs, components, packages, classes, class hierarchies,
etc.
• interfaces, protocols (where not part of the system
architecture)
• algorithms, data structures, security mechanisms,
operational procedures
If the program design is done properly, all significant design
decisions should be made before implementation.
Implementation should focus on the actual coding.

2
9/26/24

International University, VNU-HCMC

UML Models
UML models (diagrams and specifications) can be used for
almost all aspects of program design.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Diagrams give a general overview of the design, showing


the principal elements and how they relate to each other.
• Specifications provides details about each element of the
design. The specification should have sufficient detail that
they can be used to write code from.
In heavyweight software development processes, the entire
specification is completed before coding begins.
In lightweight software development processes, an outline
specification is made before coding, but the details are
completed as part of the coding process, using language based
tools such as Javadocs.

International University, VNU-HCMC

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

Models used mainly for systems architecture


• Component diagram shows the organization and
dependencies among a set of components.
• Deployment diagram shows the configuration of processing
nodes and the components that live on them.
Models used mainly for program design
• Class diagram shows a set of classes, interfaces, and
collaborations with their relationships.
• Object diagram or sequence diagram show a set of
objects and their relationships.

3
9/26/24

International University, VNU-HCMC

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]

Note on terminology. This course uses the term methods for


the operations that a class supports. UML uses the less familiar
term operations for this purpose
7

International University, VNU-HCMC

The "Hello, World!" Applet

import java.awt.Graphics;
class HelloWorld extends java.applet.Applet {
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

public void paint (Graphics g) {


g.drawString ("Hello, World!", 10, 20);
}
}

Example from: BRJ

4
9/26/24

International University, VNU-HCMC

The HelloWorld Class


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

class
HelloWorld
name

methods paint()

International University, VNU-HCMC

The HelloWorld Class


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

class

name HelloWorld optional annotation

methods paint() g.drawString ("HelloWorld", 10, 20)

10

5
9/26/24

International University, VNU-HCMC

Notation: Relationships
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

A dependency is a semantic relationship between two things in


which a change to one may effect the semantics of the other.

child parent
A generalization is a relationship in which objects of the
specialized element (child) are substitutable for objects of
the generalized element (parent).

A realization is a semantic relationship between


classifiers, wherein one classifier specifies a contract that
another classifier guarantees to carry out.
11

International University, VNU-HCMC

The HelloWorld Class

Note that the Applet and


Graphics classes are shown
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Applet elided, i.e., just the name is


shown, not the attributes or
generalization operations.

HelloWorld
dependency
Graphics
paint()

12

6
9/26/24

International University, VNU-HCMC

Notation: Association
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

0..1 *
employer employee

An association is a structural relationship that describes


a set of links, a link being a connection among objects.

13

International University, VNU-HCMC

Association
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

ParkingLot
1

1 ... *

ParkingSpace
location
is_available()

14

7
9/26/24

International University, VNU-HCMC

Rational Rose: A Typical Class Diagram


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

International University, VNU-HCMC

Specification Fields
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

8
9/26/24

International University, VNU-HCMC

Deciding which Classes to Use


Given a real-life system, how do you decide what
classes to use?
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Step 1. Identify a set of candidate classes that represent


the system design.
• What terms do the users and implementers use to
describe the system? These terms are candidates for
classes.
• Is each candidate class crisply defined?
• For each class, what is its set of responsibilities? Are
the responsibilities evenly balanced among the classes?
• What attributes and methods does each class need
to carry out its responsibilities?

17

International University, VNU-HCMC

Deciding which Classes to Use


Step 2. Modify the set of classes
Goals:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Improve the clarity of the design


If the purpose of each class is clear, with easily
understood methods and relationships, developers are
likely to write simple code that future maintainers
can understand and modify.
• Increase coherence within classes and lower coupling
between classes.
Aim for high cohesion within classes and weak coupling
between them.

18

9
9/26/24

International University, VNU-HCMC

Application Classes and Solution Classes


A good design is often a combination of application
classes and solution classes.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Application classes represent application concepts.


Noun identification is an effective technique to
generate candidate application classes.
• Solution classes represent system concepts, e.g.,
user interface objects, databases, etc.

19

International University, VNU-HCMC

Noun Identification: a Library Example


The library contains books and journals. It may have
several copies of a given book. Some of the books are
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

reserved for short-term loans only. All others may be


borrowed by any library member for three weeks.
Members of the library can normally borrow up to six
items at a time, but members of staff may borrow up to
12 items at one time. Only members of staff may borrow
journals.
The system must keep track of when books and
journals are borrowed and returned, and enforce the
rules.

20

10
9/26/24

International University, VNU-HCMC

Noun Identification: a Library Example


The library contains books and journals. It may have
several copies of a given book. Some of the books are
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

reserved for short-term loans only. All others may be


borrowed by any library member for three weeks.
Members of the library can normally borrow up to six
items at a time, but members of staff may borrow up to
12 items at one time. Only members of staff may borrow
journals.
The system must keep track of when books and
journals are borrowed and returned, and enforce the
rules.

21

International University, VNU-HCMC

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

International University, VNU-HCMC

Relations between Classes


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

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

International University, VNU-HCMC

Methods
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

LibraryMember borrows Copy


LibraryMember returns Copy
MemberOfStaff borrows Journal
MemberOfStaff returns Journal
Item not needed yet.

24

12
9/26/24

International University, VNU-HCMC

A Possible Class Diagram

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

International University, VNU-HCMC

From Candidate Classes to Completed Design

Methods used to move to final design


•Reuse: Wherever possible, use existing components or
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

class libraries. They may need extensions.


•Restructuring: Changing the design to improve
understandability, maintainability, etc. Techniques
include merging similar classes and splitting complex
classes.
•Optimization: Ensure the system meets anticipated
performance requirements, e.g., by changed
algorithms or restructuring.
•Completion: Fill all gaps, specify interfaces, etc.

26

13
9/26/24

International University, VNU-HCMC

From Candidate Classes to Completed Design

Design is iterative
• As the process moves from preliminary design to
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

specification, implementation, and testing it is common


to find weaknesses in the program design. Be prepared
to make major modifications.

27

International University, VNU-HCMC

Modeling Dynamic Aspects of Systems


Interaction diagram: shows set of objects and their
relationships including messages that may be dispatched
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

among them.
• Sequence diagrams: Time ordering of messages

28

14
9/26/24

International University, VNU-HCMC

Interaction: Informal Bouncing Ball Diagrams


Example: execution of an HTTP get command,
e.g., http://www.cs.cornell.edu/
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

domain name
service

TCP connection

HTTP get

Client Servers

29

International University, VNU-HCMC

UML Notation for Classes and Objects


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

30

15
9/26/24

International University, VNU-HCMC

Notation: Interaction
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

display

An interaction is a behavior that comprises a set of


messages exchanged among a set of objects within a
particular context to accomplish a specific purpose.

31

International University, VNU-HCMC

Actions on Objects
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

32

16
9/26/24

International University, VNU-HCMC

Sequence Diagram: Borrow Copy of a Book


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

33

International University, VNU-HCMC

Sequence Diagram: Change in Cornell Program


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

34

17
9/26/24

International University, VNU-HCMC

Sequence Diagram: Painting Mechanism


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

35

International University, VNU-HCMC

Outlines
• Models for program design
• Program development
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Reuse and design patterns

36

18
9/26/24

International University, VNU-HCMC

Integrated Development Environments


Basic software development requires:
• text editor (e.g., vi editor for Linux)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• compiler for individual files

• build system (e.g., make for Linux)


Integrated development environments combine:
• source code editor

• incremental compiler

• build automation tools

• a debugger

• and much, much more

37

International University, VNU-HCMC

Integrated Development Environments


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

19
9/26/24

International University, VNU-HCMC

Integrated Development Environment: Eclipse


Eclipse is a modern integrated development environment. It was
originally created by IBM’s Rational division. There are versions
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

for many languages including Java, C/C++, Python, etc.


The Java system provides:
• source code editor
• debugger
• incremental compiler
• programming documentation
• build automation tools
• version control
• XML editor and tools
• web development tools
Much more is available via plug-ins

39

International University, VNU-HCMC

Program Design: Integrated Development Environment

Integrated development environments provide little


help in designing a program.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

They assume that you have already have a design:


• classes
• methods
• data structures
• interfaces

40

20
9/26/24

International University, VNU-HCMC


Program Design:
Integrated Development Environment
Options for program design:
• program design using modeling tools, such as UML
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• design while coding: design — code — redesign loop


(small programs only)
• existing frameworks
• advanced environments that combine frameworks and
development tools
It is often good to combine aspects of these different
approaches

41

International University, VNU-HCMC

The Design — Code — Redesign Loop


If the class structure is straightforward it may be possible to
use the integrated development environment to:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

- create an outline of the class structure and interfaces


- write code
- modify the class structure as needed and rework the code
as necessary
This is only possible with small teams with close
communication.
The maximum size of the program depends on the experience
of programmer(s) and the complexity of the program.
It may be possible to complete a single agile sprint.
Eventually, the amount of rework becomes overwhelming.

42

21
9/26/24

International University, VNU-HCMC

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

International University, VNU-HCMC

Class Hierarchies: Programming Languages


Example: Java
Java is a relatively straightforward language with a
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

very rich set of class hierarchies.


• Java programs derive much of their functionality from
standard classes.
• Learning and understanding the classes is difficult.
• Experienced Java programmers can write complex
systems quickly.
• Inexperienced Java programmers write inelegant and
buggy programs.

44

22
9/26/24

International University, VNU-HCMC

Class Hierarchies: Programming Languages


Languages such as Java and Python steadily change their
class hierarchies over time. Commonly the changes
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

replace special purpose functionality with more general


frameworks.
If you design your programs to use the class hierarchies
in the style intended by the language developers, it is
likely to help with long term maintenance.

45

International University, VNU-HCMC

Web Development Frameworks


A web development framework provides a skeleton for
building web applications.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

An early example was Cold Fusion, which implements a three-


tier architecture.
Modern examples, such as Ruby on Rails and Django, often
use an MVC architecture.
For example, Ruby on Rails provides:
•a database
•a web server
• web pages
It is intended to be used with web standards, e.g., XML, HTML,
CSS, and JavaScript.
46

23
9/26/24

International University, VNU-HCMC

Web Development Frameworks: Django


Django is a Python framework for developing websites
• loosely based on MVC architecture
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• supports a variety of web and database servers


• web template system
• authentication system
• administrative interface
• mitigation of web attacks
Django is a complex framework. Teams should allow
plenty of time for learning.

47

International University, VNU-HCMC

Advanced Development Environments


Application frameworks can be used with any program
development environment, e.g., Django and Eclipse
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

(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

International University, VNU-HCMC

Advanced Development Environments: Xcode


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

International University, VNU-HCMC

Advanced Development Environments


An advanced development environment is intended to
provide everything that a developer needs.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

The developer is expected to follow the program choices


that are provided.
For example, when Xcode is used with iOS it has a very
specific purpose: mobile apps for Apple devices such as
iPhones, iPads.
• Special programming language (Swift or Objective C)
• MVC framework (Apple version)

50

25
9/26/24

International University, VNU-HCMC

Advanced Development Environments


If you accept the overall program design it is very
powerful:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• The auto layout of graphical interfaces


•A comprehensive set of classes for user interfaces and
navigation
• Simulators for all Apple devices

51

International University, VNU-HCMC

Using Development Frameworks


Development frameworks are powerful and flexible.
If your application fits the framework, they do much of
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

the program design and provide high quality code for


many of the standard parts of any application.
Some parts of the application may need be designed
separately.
But beware:
• You are forced to build your application within the
framework that is provided.
• The frameworks are continually modified.
• These frameworks are complex and take a long time to
learn.
52

26
9/26/24

International University, VNU-HCMC

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

International University, VNU-HCMC

Production Programming
Robust programming
• Write simple code.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Avoid risky programming constructs.


• If code is difficult to read, rewrite it.
• Incorporate redundant code to check system state
after modifications.
• Test implicit assumptions explicitly, e.g., check all
parameters received from other routines.
• Eliminate all warnings from source code.
• Have a thorough set of test cases for all your code.
In a production environment, expect to spend longer on
coding and testing than in an academic setting.

54

27
9/26/24

International University, VNU-HCMC

Outlines
• Models for program design
• Program development
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Reuse and design patterns

55

International University, VNU-HCMC

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

International University, VNU-HCMC

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

International University, VNU-HCMC

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

security weaknesses, especially when incorporated in


unusual applications.
The software is likely to be much better than a new
development team would write.
But sometimes it is sensible to write code for a
narrowly defined purpose rather than use general
purpose software.

58

29
9/26/24

International University, VNU-HCMC

Evaluating Software
Maintenance
When evaluating software, both commercial and open
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

source, pay attention to maintenance. Is the software


supported by an organization that will continue
maintenance over the long term?

59

International University, VNU-HCMC

Reuse: Open-Source Software


Open-source software varies enormously in quality.
• Because of the processes for reporting and fixing
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

problems, major systems such as Linux, Apache, Python,


Lucene, etc. tend to be very robust and free from
problems. They are often better than the commercial
equivalents.
• More experimental systems, such as Hadoop, have solid
cores, but their lesser-used features have not been
subject to the rigorous quality control of the best
software products.
• Other open-source software is of poor quality and
should not be incorporated into production systems.
60

30
9/26/24

International University, VNU-HCMC

Evaluating Applications Packages


Companies such as SAP and Oracle provide application
packages for business functions.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

They provide enormous capabilities and relieve an


organization from such tasks as updating financial
systems when laws change.
They are very expensive:
• License fees to the vendor.
• Modifications to existing systems and special code
from the vendor.
• Disruption to the organization when installing them.
• Long-term maintenance costs.
• The costs of changing to a different vendor are huge.

61

International University, VNU-HCMC

Evaluating Applications Packages


Cornell’s decision (about 1990) to move to PeopleSoft
(now part of Oracle) has cost the university several
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

hundred millions of dollars.


If you are involved in such a decision insist on a very
thorough feasibility study. Be prepared to take a least a
year and spend several million dollars before making
the decision.

62

31
9/26/24

International University, VNU-HCMC

Design for Change: Replacement of Components

The software design should anticipate possible changes


in the system over its life-cycle.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

New vendor or new technology


Components are replaced because its supplier goes out
of business, ceases to provide adequate support,
increases its price, etc., or because software from
another source provides better functionality, support,
pricing, etc.
This can apply to either open source or vendor-supplied
components.

63

International University, VNU-HCMC

Design for Change: Replacement of Components

New implementation
The original implementation may be problematic,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

e.g., poor performance, inadequate back-up and


recovery, difficult to trouble - shoot, or unable to
support growth and new features added to the
system.
Example. The portal nsdl.org was originally implemented
using uPortal. This did not support important extensions
that were requested and proved awkward to maintain. It
was reimplemented using PHP/MySQL.

64

32
9/26/24

International University, VNU-HCMC

Design for Change: Replacement of Components

Additions to the requirements


When a system goes into production, it is usual to
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

reveal both weaknesses and opportunities for extra


functionality and enhancement to the user interface
design.
For example, in a data-intensive system it is almost
certain that there will be requests for extra reports and
ways of analyzing the data.
Requests for enhancements are often the sign of a
successful system. Clients recognize latent
possibilities.

65

International University, VNU-HCMC

Design for Change: Replacement of Components

Changes in the application domain


Most application domains change continually, e.g.,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

because of business opportunities, external changes


(such as new laws), mergers and take-overs, new
groups of users, new technology, etc., etc.,
It is rarely feasible to implement a completely new
system when the application domain changes.
Therefore, existing systems must be modified. This may
involve extensive restructuring, but it is important to
reuse existing code as much as possible.

66

33
9/26/24

International University, VNU-HCMC

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

situations where classes are likely to be reused in an


evolving system.

67

International University, VNU-HCMC

Design Patterns
Sources:
1. E. Gamma, R. Helm, R. Johnson, and J. Vlissides,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Design Patterns: Elements of Reusable Object-


Oriented Software. Addison-Wesley, 1994
2. The following discussion of design patterns is based
on Gamma, et al., 1994, and Bruegge and Dutoit,
2004.
3. Wikipedia has good discussion of many design
patterns, using UML and other notation, with
code samples.

68

34
9/26/24

International University, VNU-HCMC

Structural
These concern class and object composition. They use
inheritance to compose interfaces and define ways to
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

compose objects to obtain new functionality.


• An adapter allows classes with incompatible interfaces
to work together by wrapping its interface around that
of an already existing class.
• Bridge decouples an abstraction from its
implementation so that the two can vary independently.
• Composite composes zero or more similar objects so
that they can be manipulated as one object.

69

International University, VNU-HCMC

Structural
• Decorator dynamically adds/overrides behaviour in an
existing method of an object.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Facade provides a simplified interface to a large body


of code.
• Flyweight reduces the cost of creating and
manipulating a large number of similar objects.
• Proxy provides a placeholder for another object to
control access, reduce cost, and reduce complexity.

70

35
9/26/24

International University, VNU-HCMC

Inheritance and Abstract Classes


Design patterns make extensive use of inheritance and
abstract classes.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Classes can be defined in terms of other classes using


inheritance. The generalization class is called the
superclass, and the specialization is called the subclass.
Abstract class
Abstract classes are superclasses that contain abstract
methods and are defined so that concrete subclasses
extend them by implementing abstract methods. Before a
class derived from an abstract class can be instantiated it
must implement concrete methods for all the abstract
methods of its parent classes.

71

International University, VNU-HCMC

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

International University, VNU-HCMC

Notation

ClassName class name in italic indicates an abstract class


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

dependency
delegation

inheritance

73

International University, VNU-HCMC

Adapter (Wrapper): Wrapping Around Legacy Code

Problem description:
Convert the interface of a legacy class into a different
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

interface expected by the client, so that the client


and the legacy class can work together without
changes.
This problem often occurs during a transitional
period, when the long-term plan is to phase out the
legacy system.
Example:
How do you use a web browser to access an
information retrieval system that was designed for a
different client?
74

37
9/26/24

International University, VNU-HCMC

Adapter Design Pattern: The Problem


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

NewClient OldClient

dependency

NewClass LegacyClass
request() existingRequest()

During the transition, how can NewClient be used


with LegacyClass?

75

International University, VNU-HCMC

Adapter Design Pattern: Solution Class Diagram

Client abstract class shown in


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

italic

ClientInterface LegacyClass
request() existingRequest()

Adapter delegation
inheritance request()

76

38
9/26/24

International University, VNU-HCMC

Adapter Design Pattern: Consequences

The following consequences apply whenever the Adapter


design pattern is used.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Client and LegacyClass work together without modification


of either.
Adapter works with LegacyClass and all of its subclasses.
A new Adapter needs to be written if Client is replaced
by a subclass.

77

International University, VNU-HCMC

Bridge: Allowing for Alternate Implementations

Name: Bridge design pattern


Problem description:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Decouple an interface from an implementation so that


a different implementation can be substituted, possibly
at runtime (e.g., testing different implementations of
the same interface).

78

39
9/26/24

International University, VNU-HCMC

Bridge: Class Diagram


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Client

alternative
implementations

ConcreteImplementorA

ConcreteImplementorB

79

International University, VNU-HCMC

Bridge: Class Diagram


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Client

Implementor

ConcreteImplementorA

ConcreteImplementorB

80

40
9/26/24

International University, VNU-HCMC

Bridge: Class Diagram

Client
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Abstraction Implementor

Note the similarity


ConcreteImplementorA
to the strategy
design pattern ConcreteImplementorB

(described later)

81

International University, VNU-HCMC

Bridge: Allowing for Alternate Implementations

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

International University, VNU-HCMC

Bridge: Consequences
• Client is shielded from abstract and concrete
implementations
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Interfaces and implementations can be tested


separately

83

International University, VNU-HCMC

Strategy: Encapsulating Algorithms


Name: Strategy design pattern
Example:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

A mobile computer can be used with a wireless


network, or connected to an Ethernet, with dynamic
switching between networks based on location and
network costs.
Problem description:
Decouple a policy-deciding class from a set of
mechanisms, so that different mechanisms can be
changed transparently.

84

42
9/26/24

International University, VNU-HCMC

Strategy Example: Class Diagram for Mobile Computer


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

85

International University, VNU-HCMC

Strategy Example: Class Diagram for Mobile Computer


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

86

43
9/26/24

International University, VNU-HCMC

Strategy Example: Class Diagram for Mobile Computer

Application LocationManager use location information


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

to select network
NetworkInterface
NetworkConnection
open()
Open() close()
Close() send()
Send()
setNetworkInterface()

Ethernet WirelessNet
open() open()
close() close()
send() send()

87

International University, VNU-HCMC

Strategy: Encapsulating Algorithms


Solution:
• A Client accesses services provided by a Context.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• The Context services are realized using one of several


mechanisms, as decided by a Policy object.
• The abstract class Strategy describes the interface that
is common to all mechanisms that Context can use.
Policy class creates a ConcreteStrategy object and
configures Context to use it.

88

44
9/26/24

International University, VNU-HCMC

Strategy: Class Diagram

Application Policy Policy class selects


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

ConcreteStrategy

Context
ContextInterface() Strategy
algorithmInterface()

Note the similarity to the


bridge design pattern
(described above) ConcreteStrategy1 ConcreteStrategy2

89

International University, VNU-HCMC

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

International University, VNU-HCMC

Facade: Encapsulating Subsystems


Name: Facade design pattern
Problem description:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Reduce coupling between a set of related classes and the rest


of the system.
Example:
A Compiler is composed of several classes: LexicalAnalyzer,
Parser, CodeGenerator, etc. A caller invokes only the Compiler
(Facade) class, which invokes the contained classes.
Solution:
A single Facade class implements a high-level interface for
a subsystem by invoking the methods of the lower-level
classes.
91

International University, VNU-HCMC

Facade: Class Diagram


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Facade
Facade
service2()

Class1 Class2 Class3


service1() service2() service3()

92

46
9/26/24

International University, VNU-HCMC

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

International University, VNU-HCMC

Composite: Representing Recursive Hierarchies

Name: Composite design pattern


Problem description:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Represent a hierarchy of variable width and depth, so


that the leaves and composites can be treated
uniformly through a common interface.

94

47
9/26/24

International University, VNU-HCMC

Composite: Class Diagram


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Client

Component

Leaf Composite

95

International University, VNU-HCMC

Composite: Representing Recursive Hierarchies

Solution
• The Component interface specifies the services that
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

are shared between Leaf and Composite.


• A Composite has an aggregation association with
Components and implements each service by iterating
over each contained Component.
• The Leaf services do the actual work.

96

48
9/26/24

International University, VNU-HCMC

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

International University, VNU-HCMC

Abstract Factory: Encapsulating Platforms

Name: Abstract Factory design pattern


Problem description:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Shield the client from different platforms that


provide different implementations of the same
set of concepts
Example:
A user interface might have versions that implement the
same set of concepts for several windowing systems,
e.g., scroll bars, buttons, highlighting, etc.

98

49
9/26/24

International University, VNU-HCMC

Abstract Factory: Encapsulating Platforms

Solution:
A platform (e.g., the application for a specific
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

windowing system) is represented as a set of


AbstractProducts, each representing a concept (e.g.,
button). An AbstractFactory class declares the
operations for creating each individual product.
A specific platform is then realized by a
ConcreteFactory and a set of ConcreteProducts.

99

International University, VNU-HCMC

Abstract Factory: Class Diagram


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Client AbstractFactory

createProductA
createProductB
AbstractProductB

AbstractProductA

100

50
9/26/24

International University, VNU-HCMC

Abstract Factory: Class Diagram

Client AbstractFactory
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

createProductA

ConcreteFactory1

AbstractProductA createProductA

There could be several


ProductA
ConcreteFactory classes,
each a subclass of
AbstractFactory

101

International University, VNU-HCMC

Abstract Factory: Class Diagram

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

International University, VNU-HCMC

Abstract Factory: Consequences

• Client is shielded from concrete products classes


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Substituting families at runtime is possible


• Adding new products is difficult since new realizations
must be created for each factory

103

International University, VNU-HCMC

Discussion
See the interesting discussion in Wikipedia (December
16, 2016):
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

"Use of this pattern makes it possible to interchange


concrete classes without changing the code that uses
them, even at runtime. However, employment of this
pattern, as with similar design patterns, may result in
unnecessary complexity and extra work in the initial
writing of code."

104

52
9/26/24

International University, VNU-HCMC

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

The company already has a product database with


specification, marketing information, and prices of
the equipment that it manufactures.
To sell equipment online the company will need to
create: a customer database, and an ordering system
for online customers.
The plan is to develop the system in two phases. During
Phase 1, simple versions of the customer database and
ordering system will be brought into production. In
Phase 2, major enhancements will be made to these
components.
105

International University, VNU-HCMC

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

International University, VNU-HCMC

An Exam Question

Client
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Ordering System

OrderingAbstraction DBImplementor

ConcreteDBImplementorA

ConcreteDBImplementorB

107

International University, VNU-HCMC

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

International University, VNU-HCMC

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

update systems and been continually modified.


• Recent modifications might include customer interfaces for
the web, smartphones, etc.
• The systems will have migrated from computer to computer,
across operating systems, to different database systems,
etc.
• The organizations may have changed through mergers, etc.
Maintaining a coherent system architecture for such legacy
systems is an enormous challenge, yet the complexity of
building new systems is so great that it is rarely attempted.

109

International University, VNU-HCMC

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

International University, VNU-HCMC

Legacy Requirements
Planning
In conjunction with the client develop a plan for
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

rebuilding the system.


Requirements as seen by the customers and users
• Who are the users?
• What do they actually use the system for?
• Does the system have undocumented features that are
important or bugs that users rely on?
• How many people use the fringe parts of the system?
Where are they flexible?

111

International University, VNU-HCMC

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

say about the requirements?


• Does the source code include any hints about the
requirements?
• Is there code to support obsolete hardware or
services? If so, does anybody still use them?

112

56
9/26/24

International University, VNU-HCMC

Legacy Code
Source code management
• Use a source code management system to establish a
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

starting version of the source code and binaries that


are built from this source code.
• Create a test environment so that the rebuilt system
can be compared with the current system. Begin to
collect test cases.
• Check the licenses for all vendor software.

113

International University, VNU-HCMC

Legacy Code
Rebuilding the software
An incremental software development process is often
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

appropriate, with each increment released when


completed.
The following tasks may be tackled in any appropriate
order, based on the condition of the code. Usually, the
strategy will be to work on different parts of the system
in a series of phases.
• Understand the original systems architecture and
program design.
• Establish a component architecture, with defined
interfaces, even if much of the code violates the
architecture and needs adapters.
114

57
9/26/24

International University, VNU-HCMC

Legacy Code
• Move to current versions of programming languages and
systems software.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• If there are any subsystems that do not have source


code, carry out a development cycle to create new
code that implements the requirements.
• If there is duplicate code, replace with a single version.
• Remove redundant code and obsolete requirements.
Clean up as you go along.

115

International University, VNU-HCMC


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Thank you for your attention!

116

58

You might also like